Commit df00ebde authored by Sean McGivern's avatar Sean McGivern

DRY up caching in AbstractReferenceFilter

We had a lot of copied and pasted code, when the different elements were very
small and easy to get wrong.
parent 76b80d6e
...@@ -54,9 +54,9 @@ module Banzai ...@@ -54,9 +54,9 @@ module Banzai
self.class.references_in(*args, &block) self.class.references_in(*args, &block)
end end
# Implement in child class
# Example: project.merge_requests.find
def find_object(project, id) def find_object(project, id)
# Implement in child class
# Example: project.merge_requests.find
end end
# Override if the link reference pattern produces a different ID (global # Override if the link reference pattern produces a different ID (global
...@@ -65,47 +65,31 @@ module Banzai ...@@ -65,47 +65,31 @@ module Banzai
find_object(project, id) find_object(project, id)
end end
def find_object_cached(project, id) # Implement in child class
if RequestStore.active? # Example: project_merge_request_url
cache = find_objects_cache[object_class][project.id] def url_for_object(object, project)
end
get_or_set_cache(cache, id) { find_object(project, id) } def find_object_cached(project, id)
else cached_call(:banzai_find_object, id, path: [object_class, project.id]) do
find_object(project, id) find_object(project, id)
end end
end end
def find_object_from_link_cached(project, id) def find_object_from_link_cached(project, id)
if RequestStore.active? cached_call(:banzai_find_object_from_link, id, path: [object_class, project.id]) do
cache = find_objects_from_link_cache[object_class][project.id]
get_or_set_cache(cache, id) { find_object_from_link(project, id) }
else
find_object_from_link(project, id) find_object_from_link(project, id)
end end
end end
def project_from_ref_cached(ref) def project_from_ref_cached(ref)
if RequestStore.active? cached_call(:banzai_project_refs, ref) do
cache = project_refs_cache
get_or_set_cache(cache, ref) { project_from_ref(ref) }
else
project_from_ref(ref) project_from_ref(ref)
end end
end end
def url_for_object(object, project)
# Implement in child class
# Example: project_merge_request_url
end
def url_for_object_cached(object, project) def url_for_object_cached(object, project)
if RequestStore.active? cached_call(:banzai_url_for_object, object, path: [object_class, project.id]) do
cache = url_for_object_cache[object_class][project.id]
get_or_set_cache(cache, object) { url_for_object(object, project) }
else
url_for_object(object, project) url_for_object(object, project)
end end
end end
...@@ -324,21 +308,17 @@ module Banzai ...@@ -324,21 +308,17 @@ module Banzai
RequestStore[:banzai_project_refs] ||= {} RequestStore[:banzai_project_refs] ||= {}
end end
def find_objects_cache def cached_call(request_store_key, cache_key, path: [])
RequestStore[:banzai_find_objects_cache] ||= Hash.new do |hash, key| if RequestStore.active?
hash[key] = Hash.new { |h, k| h[k] = {} } cache = RequestStore[request_store_key] ||= Hash.new do |hash, key|
end hash[key] = Hash.new { |h, k| h[k] = {} }
end end
def find_objects_from_link_cache cache = cache.dig(*path) if path.any?
RequestStore[:banzai_find_objects_from_link_cache] ||= Hash.new do |hash, key|
hash[key] = Hash.new { |h, k| h[k] = {} }
end
end
def url_for_object_cache get_or_set_cache(cache, cache_key) { yield }
RequestStore[:banzai_url_for_object] ||= Hash.new do |hash, key| else
hash[key] = Hash.new { |h, k| h[k] = {} } yield
end end
end end
......
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