2017-07-17 11:24:46 -04:00
|
|
|
module Gitlab
|
|
|
|
module Cache
|
|
|
|
# This module provides a simple way to cache values in RequestStore,
|
|
|
|
# and the cache key would be based on the class name, method name,
|
2017-07-17 13:30:58 -04:00
|
|
|
# optionally customized instance level values, optionally customized
|
|
|
|
# method level values, and optional method arguments.
|
2017-07-17 11:24:46 -04:00
|
|
|
#
|
|
|
|
# A simple example:
|
|
|
|
#
|
|
|
|
# class UserAccess
|
2017-07-18 05:48:48 -04:00
|
|
|
# extend Gitlab::Cache::RequestCache
|
2017-07-17 11:24:46 -04:00
|
|
|
#
|
2017-07-18 06:04:20 -04:00
|
|
|
# request_cache_key do
|
2017-07-17 13:30:58 -04:00
|
|
|
# [user&.id, project&.id]
|
2017-07-17 11:24:46 -04:00
|
|
|
# end
|
|
|
|
#
|
2017-07-18 06:04:20 -04:00
|
|
|
# request_cache def can_push_to_branch?(ref)
|
2017-07-17 11:24:46 -04:00
|
|
|
# # ...
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# This way, the result of `can_push_to_branch?` would be cached in
|
2017-07-17 13:30:58 -04:00
|
|
|
# `RequestStore.store` based on the cache key. If RequestStore is not
|
|
|
|
# currently active, then it would be stored in a hash saved in an
|
|
|
|
# instance variable, so the cache logic would be the same.
|
|
|
|
# Here's another example using customized method level values:
|
|
|
|
#
|
|
|
|
# class Commit
|
2017-07-18 05:48:48 -04:00
|
|
|
# extend Gitlab::Cache::RequestCache
|
2017-07-17 13:30:58 -04:00
|
|
|
#
|
|
|
|
# def author
|
|
|
|
# User.find_by_any_email(author_email.downcase)
|
|
|
|
# end
|
2017-07-18 06:04:20 -04:00
|
|
|
# request_cache(:author) { author_email.downcase }
|
2017-07-17 13:30:58 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# So that we could have different strategies for different methods
|
|
|
|
#
|
2017-07-18 05:48:48 -04:00
|
|
|
module RequestCache
|
2017-07-17 11:24:46 -04:00
|
|
|
def self.extended(klass)
|
|
|
|
return if klass < self
|
|
|
|
|
|
|
|
extension = Module.new
|
2017-07-18 05:48:48 -04:00
|
|
|
klass.const_set(:RequestCacheExtension, extension)
|
2017-07-17 11:24:46 -04:00
|
|
|
klass.prepend(extension)
|
|
|
|
end
|
|
|
|
|
2017-07-18 06:04:20 -04:00
|
|
|
def request_cache_key(&block)
|
2017-07-17 11:24:46 -04:00
|
|
|
if block_given?
|
2017-07-18 06:04:20 -04:00
|
|
|
@request_cache_key = block
|
2017-07-17 11:24:46 -04:00
|
|
|
else
|
2017-07-18 06:04:20 -04:00
|
|
|
@request_cache_key
|
2017-07-17 11:24:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-18 06:04:20 -04:00
|
|
|
def request_cache(method_name, &method_key_block)
|
2017-07-18 05:48:48 -04:00
|
|
|
const_get(:RequestCacheExtension).module_eval do
|
|
|
|
cache_key_method_name = "#{method_name}_cache_key"
|
|
|
|
|
2017-07-17 13:18:20 -04:00
|
|
|
define_method(method_name) do |*args|
|
|
|
|
store =
|
|
|
|
if RequestStore.active?
|
|
|
|
RequestStore.store
|
|
|
|
else
|
|
|
|
ivar_name = # ! and ? cannot be used as ivar name
|
2017-07-18 05:48:48 -04:00
|
|
|
"@cache_#{method_name.to_s.tr('!?', "\u2605\u2606")}"
|
2017-07-17 11:24:46 -04:00
|
|
|
|
2017-07-17 13:18:20 -04:00
|
|
|
instance_variable_get(ivar_name) ||
|
|
|
|
instance_variable_set(ivar_name, {})
|
|
|
|
end
|
|
|
|
|
2017-07-18 05:48:48 -04:00
|
|
|
key = __send__(cache_key_method_name, args)
|
2017-07-17 11:24:46 -04:00
|
|
|
|
2017-07-18 05:48:48 -04:00
|
|
|
store.fetch(key) { store[key] = super(*args) }
|
2017-07-17 11:24:46 -04:00
|
|
|
end
|
2017-07-17 13:18:20 -04:00
|
|
|
|
|
|
|
define_method(cache_key_method_name) do |args|
|
|
|
|
klass = self.class
|
|
|
|
|
2017-07-18 06:04:20 -04:00
|
|
|
instance_key = instance_exec(&klass.request_cache_key) if
|
|
|
|
klass.request_cache_key
|
2017-07-17 13:18:20 -04:00
|
|
|
|
|
|
|
method_key = instance_exec(&method_key_block) if method_key_block
|
|
|
|
|
|
|
|
[klass.name, method_name, *instance_key, *method_key, *args]
|
|
|
|
.join(':')
|
|
|
|
end
|
|
|
|
|
|
|
|
private cache_key_method_name
|
|
|
|
end
|
2017-07-17 11:24:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|