diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2017-07-18 12:48:48 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2017-07-18 12:48:48 +0300 |
commit | ffc5b29bd0f02676bdc05ec6185d115d6705cd8f (patch) | |
tree | 43f3c0e86116207bcde2105b5df41c205b1ace49 /lib | |
parent | 042cf15b6f6dae6a6e3f760f93a3ad0f7ff486ea (diff) |
Follow feedback on the merge request
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/cache/request_cache.rb (renamed from lib/gitlab/cache/request_store_wrap.rb) | 24 | ||||
-rw-r--r-- | lib/gitlab/user_access.rb | 2 |
2 files changed, 11 insertions, 15 deletions
diff --git a/lib/gitlab/cache/request_store_wrap.rb b/lib/gitlab/cache/request_cache.rb index e10ff235934..b012c876560 100644 --- a/lib/gitlab/cache/request_store_wrap.rb +++ b/lib/gitlab/cache/request_cache.rb @@ -8,7 +8,7 @@ module Gitlab # A simple example: # # class UserAccess - # extend Gitlab::Cache::RequestStoreWrap + # extend Gitlab::Cache::RequestCache # # request_store_wrap_key do # [user&.id, project&.id] @@ -26,7 +26,7 @@ module Gitlab # Here's another example using customized method level values: # # class Commit - # extend Gitlab::Cache::RequestStoreWrap + # extend Gitlab::Cache::RequestCache # # def author # User.find_by_any_email(author_email.downcase) @@ -36,12 +36,12 @@ module Gitlab # # So that we could have different strategies for different methods # - module RequestStoreWrap + module RequestCache def self.extended(klass) return if klass < self extension = Module.new - klass.const_set(:RequestStoreWrapExtension, extension) + klass.const_set(:RequestCacheExtension, extension) klass.prepend(extension) end @@ -54,30 +54,26 @@ module Gitlab end def request_store_wrap(method_name, &method_key_block) - const_get(:RequestStoreWrapExtension).module_eval do + const_get(:RequestCacheExtension).module_eval do + cache_key_method_name = "#{method_name}_cache_key" + define_method(method_name) do |*args| store = if RequestStore.active? RequestStore.store else ivar_name = # ! and ? cannot be used as ivar name - "@#{method_name.to_s.tr('!', "\u2605").tr('?', "\u2606")}" + "@cache_#{method_name.to_s.tr('!?', "\u2605\u2606")}" instance_variable_get(ivar_name) || instance_variable_set(ivar_name, {}) end - key = send("#{method_name}_cache_key", args) + key = __send__(cache_key_method_name, args) - if store.key?(key) - store[key] - else - store[key] = super(*args) - end + store.fetch(key) { store[key] = super(*args) } end - cache_key_method_name = "#{method_name}_cache_key" - define_method(cache_key_method_name) do |args| klass = self.class diff --git a/lib/gitlab/user_access.rb b/lib/gitlab/user_access.rb index 9c066627011..a30dfe0f6bf 100644 --- a/lib/gitlab/user_access.rb +++ b/lib/gitlab/user_access.rb @@ -1,6 +1,6 @@ module Gitlab class UserAccess - extend Gitlab::Cache::RequestStoreWrap + extend Gitlab::Cache::RequestCache request_store_wrap_key do [user&.id, project&.id] |