diff options
author | Imre Farkas <ifarkas@gitlab.com> | 2018-06-22 15:59:36 +0300 |
---|---|---|
committer | Imre Farkas <ifarkas@gitlab.com> | 2018-06-29 18:41:57 +0300 |
commit | d4d6528c863d48c9e2d30e6b5927ef01ec35e0e3 (patch) | |
tree | 68d1079afab2b1ebaabc855425889f253cd16c48 | |
parent | 9c3214640cfb572af3cc419d159c11bcd5b5e624 (diff) |
Expire correct method caches after HEAD changed
-rw-r--r-- | app/models/repository.rb | 6 | ||||
-rw-r--r-- | changelogs/unreleased/44725-expire_correct_methods_after_change_head.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/repository_cache_adapter.rb | 10 | ||||
-rw-r--r-- | spec/lib/gitlab/repository_cache_adapter_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 28 |
5 files changed, 49 insertions, 12 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 3089d0162ee..efe7c95f786 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -283,6 +283,10 @@ class Repository ) end + def cached_methods + CACHED_METHODS + end + def expire_tags_cache expire_method_caches(%i(tag_names tag_count)) @tags = nil @@ -423,7 +427,7 @@ class Repository # Runs code after the HEAD of a repository is changed. def after_change_head - expire_method_caches(METHOD_CACHES_FOR_FILE_TYPES.keys) + expire_all_method_caches end # Runs code after a repository has been forked/imported. diff --git a/changelogs/unreleased/44725-expire_correct_methods_after_change_head.yml b/changelogs/unreleased/44725-expire_correct_methods_after_change_head.yml new file mode 100644 index 00000000000..21a65f142c3 --- /dev/null +++ b/changelogs/unreleased/44725-expire_correct_methods_after_change_head.yml @@ -0,0 +1,5 @@ +--- +title: Expire correct method caches after HEAD changed +merge_request: +author: +type: fixed diff --git a/lib/gitlab/repository_cache_adapter.rb b/lib/gitlab/repository_cache_adapter.rb index 7f64a8c9e46..2ec871f0754 100644 --- a/lib/gitlab/repository_cache_adapter.rb +++ b/lib/gitlab/repository_cache_adapter.rb @@ -25,6 +25,11 @@ module Gitlab raise NotImplementedError 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. # # The cache key and instance variable are named the same way as the value of @@ -67,6 +72,11 @@ module Gitlab # Expires the caches of a specific set of methods def expire_method_caches(methods) 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) ivar = cache_instance_variable_name(key) diff --git a/spec/lib/gitlab/repository_cache_adapter_spec.rb b/spec/lib/gitlab/repository_cache_adapter_spec.rb index 85971f2a7ef..5bd4d6c6a48 100644 --- a/spec/lib/gitlab/repository_cache_adapter_spec.rb +++ b/spec/lib/gitlab/repository_cache_adapter_spec.rb @@ -67,10 +67,18 @@ describe Gitlab::RepositoryCacheAdapter do describe '#expire_method_caches' 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) - 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 diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index d817a8376f4..4c4d61218e9 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -1699,19 +1699,29 @@ describe Repository do end 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([ - :readme, + :size, + :commit_count, + :rendered_readme, + :contribution_guide, :changelog, - :license, - :contributing, + :license_blob, + :license_key, :gitignore, - :koding, - :gitlab_ci, + :koding_yml, + :gitlab_ci_yml, + :branch_names, + :tag_names, + :branch_count, + :tag_count, :avatar, - :issue_template, - :merge_request_template, - :xcode_config + :exists?, + :root_ref, + :has_visible_content?, + :issue_template_names, + :merge_request_template_names, + :xcode_project? ]) repository.after_change_head |