diff options
Diffstat (limited to 'lib/gitlab/cache/request_store_wrap.rb')
-rw-r--r-- | lib/gitlab/cache/request_store_wrap.rb | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/lib/gitlab/cache/request_store_wrap.rb b/lib/gitlab/cache/request_store_wrap.rb index 1cb442444bb..e10ff235934 100644 --- a/lib/gitlab/cache/request_store_wrap.rb +++ b/lib/gitlab/cache/request_store_wrap.rb @@ -2,7 +2,8 @@ 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, - # customized instance level values, and arguments. + # optionally customized instance level values, optionally customized + # method level values, and optional method arguments. # # A simple example: # @@ -10,7 +11,7 @@ module Gitlab # extend Gitlab::Cache::RequestStoreWrap # # request_store_wrap_key do - # [user.id, project.id] + # [user&.id, project&.id] # end # # request_store_wrap def can_push_to_branch?(ref) @@ -19,7 +20,22 @@ module Gitlab # end # # This way, the result of `can_push_to_branch?` would be cached in - # `RequestStore.store` based on the cache key. + # `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 + # extend Gitlab::Cache::RequestStoreWrap + # + # def author + # User.find_by_any_email(author_email.downcase) + # end + # request_store_wrap(:author) { author_email.downcase } + # end + # + # So that we could have different strategies for different methods + # module RequestStoreWrap def self.extended(klass) return if klass < self |