summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-05-10 09:02:28 -0500
committerDouwe Maan <douwe@selenight.nl>2017-05-10 20:07:04 -0500
commit77798c6506aa5253e10325d78ee1b73901d80b06 (patch)
treebc6318da3ddfa8a4e2f2e15e0f5c24ff74bea456
parent08706f683725dcfbda1708ddf079347d989fbef1 (diff)
downloadgitlab-ce-77798c6506aa5253e10325d78ee1b73901d80b06.tar.gz
Reset Repository#license memo when license file is changed
-rw-r--r--app/models/repository.rb20
-rw-r--r--spec/models/repository_spec.rb3
2 files changed, 15 insertions, 8 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 2aec7237640..11163ec77e9 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -30,7 +30,7 @@ class Repository
METHOD_CACHES_FOR_FILE_TYPES = {
readme: :rendered_readme,
changelog: :changelog,
- license: %i(license_blob license_key),
+ license: %i(license_blob license_key license),
contributing: :contribution_guide,
gitignore: :gitignore,
koding: :koding_yml,
@@ -42,13 +42,13 @@ class Repository
# variable.
#
# This only works for methods that do not take any arguments.
- def self.cache_method(name, fallback: nil)
+ def self.cache_method(name, fallback: nil, memoize_only: false)
original = :"_uncached_#{name}"
alias_method(original, name)
define_method(name) do
- cache_method_output(name, fallback: fallback) { __send__(original) }
+ cache_method_output(name, fallback: fallback, memoize_only: memoize_only) { __send__(original) }
end
end
@@ -550,11 +550,11 @@ class Repository
cache_method :license_key
def license
- return @license if defined?(@license)
return unless license_key
- @license = Licensee::License.new(license_key)
+ Licensee::License.new(license_key)
end
+ cache_method :license, memoize_only: true
def gitignore
file_on_head(:gitignore)
@@ -1068,14 +1068,20 @@ class Repository
#
# key - The name of the key to cache the data in.
# fallback - A value to fall back to in the event of a Git error.
- def cache_method_output(key, fallback: nil, &block)
+ def cache_method_output(key, fallback: nil, memoize_only: false, &block)
ivar = cache_instance_variable_name(key)
if instance_variable_defined?(ivar)
instance_variable_get(ivar)
else
begin
- instance_variable_set(ivar, cache.fetch(key, &block))
+ value =
+ if memoize_only
+ yield
+ else
+ cache.fetch(key, &block)
+ end
+ instance_variable_set(ivar, value)
rescue Rugged::ReferenceError, Gitlab::Git::Repository::NoRepository
# if e.g. HEAD or the entire repository doesn't exist we want to
# gracefully handle this and not cache anything.
diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb
index 6ebc9965924..00a9f2abeb9 100644
--- a/spec/models/repository_spec.rb
+++ b/spec/models/repository_spec.rb
@@ -1858,11 +1858,12 @@ describe Repository, models: true do
describe '#refresh_method_caches' do
it 'refreshes the caches of the given types' do
expect(repository).to receive(:expire_method_caches).
- with(%i(rendered_readme license_blob license_key))
+ with(%i(rendered_readme license_blob license_key license))
expect(repository).to receive(:rendered_readme)
expect(repository).to receive(:license_blob)
expect(repository).to receive(:license_key)
+ expect(repository).to receive(:license)
repository.refresh_method_caches(%i(readme license))
end