Commit d4d6528c authored by Imre Farkas's avatar Imre Farkas

Expire correct method caches after HEAD changed

parent 9c321464
...@@ -283,6 +283,10 @@ class Repository ...@@ -283,6 +283,10 @@ class Repository
) )
end end
def cached_methods
CACHED_METHODS
end
def expire_tags_cache def expire_tags_cache
expire_method_caches(%i(tag_names tag_count)) expire_method_caches(%i(tag_names tag_count))
@tags = nil @tags = nil
...@@ -423,7 +427,7 @@ class Repository ...@@ -423,7 +427,7 @@ class Repository
# Runs code after the HEAD of a repository is changed. # Runs code after the HEAD of a repository is changed.
def after_change_head def after_change_head
expire_method_caches(METHOD_CACHES_FOR_FILE_TYPES.keys) expire_all_method_caches
end end
# Runs code after a repository has been forked/imported. # Runs code after a repository has been forked/imported.
......
---
title: Expire correct method caches after HEAD changed
merge_request:
author:
type: fixed
...@@ -25,6 +25,11 @@ module Gitlab ...@@ -25,6 +25,11 @@ module Gitlab
raise NotImplementedError raise NotImplementedError
end end
# List of cached methods. Should be overridden by the including class
def cached_methods
raise NotImplementedError
end
# Caches the supplied block both in a cache and in an instance variable. # Caches the supplied block both in a cache and in an instance variable.
# #
# The cache key and instance variable are named the same way as the value of # The cache key and instance variable are named the same way as the value of
...@@ -67,6 +72,11 @@ module Gitlab ...@@ -67,6 +72,11 @@ module Gitlab
# Expires the caches of a specific set of methods # Expires the caches of a specific set of methods
def expire_method_caches(methods) def expire_method_caches(methods)
methods.each do |key| methods.each do |key|
unless cached_methods.include?(key.to_sym)
Rails.logger.error "Requested to expire non-existent method '#{key}' for Repository"
next
end
cache.expire(key) cache.expire(key)
ivar = cache_instance_variable_name(key) ivar = cache_instance_variable_name(key)
......
...@@ -67,10 +67,18 @@ describe Gitlab::RepositoryCacheAdapter do ...@@ -67,10 +67,18 @@ describe Gitlab::RepositoryCacheAdapter do
describe '#expire_method_caches' do describe '#expire_method_caches' do
it 'expires the caches of the given methods' do it 'expires the caches of the given methods' do
expect(cache).to receive(:expire).with(:readme) expect(cache).to receive(:expire).with(:rendered_readme)
expect(cache).to receive(:expire).with(:gitignore) expect(cache).to receive(:expire).with(:gitignore)
repository.expire_method_caches(%i(readme gitignore)) repository.expire_method_caches(%i(rendered_readme gitignore))
end
it 'does not expire caches for non-existent methods' do
expect(cache).not_to receive(:expire).with(:nonexistent)
expect(Rails.logger).to(
receive(:error).with("Requested to expire non-existent method 'nonexistent' for Repository"))
repository.expire_method_caches(%i(nonexistent))
end end
end end
end end
...@@ -1699,19 +1699,29 @@ describe Repository do ...@@ -1699,19 +1699,29 @@ describe Repository do
end end
describe '#after_change_head' do describe '#after_change_head' do
it 'flushes the readme cache' do it 'flushes the method caches' do
expect(repository).to receive(:expire_method_caches).with([ expect(repository).to receive(:expire_method_caches).with([
:readme, :size,
:commit_count,
:rendered_readme,
:contribution_guide,
:changelog, :changelog,
:license, :license_blob,
:contributing, :license_key,
:gitignore, :gitignore,
:koding, :koding_yml,
:gitlab_ci, :gitlab_ci_yml,
:branch_names,
:tag_names,
:branch_count,
:tag_count,
:avatar, :avatar,
:issue_template, :exists?,
:merge_request_template, :root_ref,
:xcode_config :has_visible_content?,
:issue_template_names,
:merge_request_template_names,
:xcode_project?
]) ])
repository.after_change_head repository.after_change_head
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment