summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2016-04-04 11:39:11 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2016-04-04 11:39:11 +0200
commit915fd3f910921bc97abdda5f2ae63093c11533fd (patch)
treea075a55f2b6e88c618b9a479bbf73413ff709e16 /lib
parentede351a99e33d825b7c1421388d2cce2ca1278d5 (diff)
downloadgitlab-ce-915fd3f910921bc97abdda5f2ae63093c11533fd.tar.gz
Cleaned up caching in AbstractReferenceFilter
Cleaning this up any further is a bit tricky as the caches in question should only be evaluated if RequestStore is actually enabled.
Diffstat (limited to 'lib')
-rw-r--r--lib/banzai/filter/abstract_reference_filter.rb26
1 files changed, 11 insertions, 15 deletions
diff --git a/lib/banzai/filter/abstract_reference_filter.rb b/lib/banzai/filter/abstract_reference_filter.rb
index 02ef27cf577..f21dbef216c 100644
--- a/lib/banzai/filter/abstract_reference_filter.rb
+++ b/lib/banzai/filter/abstract_reference_filter.rb
@@ -74,11 +74,7 @@ module Banzai
if RequestStore.active?
cache = find_objects_cache[object_class][project.id]
- if cache.key?(id)
- cache[id]
- else
- cache[id] = find_object(project, id)
- end
+ get_or_set_cache(cache, id) { find_object(project, id) }
else
find_object(project, id)
end
@@ -88,11 +84,7 @@ module Banzai
if RequestStore.active?
cache = project_refs_cache
- if cache.key?(ref)
- cache[ref]
- else
- cache[ref] = project_from_ref(ref)
- end
+ get_or_set_cache(cache, ref) { project_from_ref(ref) }
else
project_from_ref(ref)
end
@@ -107,11 +99,7 @@ module Banzai
if RequestStore.active?
cache = url_for_object_cache[object_class][project.id]
- if cache.key?(object)
- cache[object]
- else
- cache[object] = url_for_object(object, project)
- end
+ get_or_set_cache(cache, object) { url_for_object(object, project) }
else
url_for_object(object, project)
end
@@ -243,6 +231,14 @@ module Banzai
hash[key] = Hash.new { |h, k| h[k] = {} }
end
end
+
+ def get_or_set_cache(cache, key)
+ if cache.key?(key)
+ cache[key]
+ else
+ cache[key] = yield
+ end
+ end
end
end
end