Commit ffc5b29b authored by Lin Jen-Shin's avatar Lin Jen-Shin

Follow feedback on the merge request

parent 042cf15b
class Commit class Commit
extend ActiveModel::Naming extend ActiveModel::Naming
extend Gitlab::Cache::RequestStoreWrap extend Gitlab::Cache::RequestCache
include ActiveModel::Conversion include ActiveModel::Conversion
include Noteable include Noteable
......
--- ---
title: Add RequestStoreWrap which makes caching with RequestStore easier title: Add RequestCache which makes caching with RequestStore easier
merge_request: 12920 merge_request: 12920
author: author:
...@@ -8,7 +8,7 @@ module Gitlab ...@@ -8,7 +8,7 @@ module Gitlab
# A simple example: # A simple example:
# #
# class UserAccess # class UserAccess
# extend Gitlab::Cache::RequestStoreWrap # extend Gitlab::Cache::RequestCache
# #
# request_store_wrap_key do # request_store_wrap_key do
# [user&.id, project&.id] # [user&.id, project&.id]
...@@ -26,7 +26,7 @@ module Gitlab ...@@ -26,7 +26,7 @@ module Gitlab
# Here's another example using customized method level values: # Here's another example using customized method level values:
# #
# class Commit # class Commit
# extend Gitlab::Cache::RequestStoreWrap # extend Gitlab::Cache::RequestCache
# #
# def author # def author
# User.find_by_any_email(author_email.downcase) # User.find_by_any_email(author_email.downcase)
...@@ -36,12 +36,12 @@ module Gitlab ...@@ -36,12 +36,12 @@ module Gitlab
# #
# So that we could have different strategies for different methods # So that we could have different strategies for different methods
# #
module RequestStoreWrap module RequestCache
def self.extended(klass) def self.extended(klass)
return if klass < self return if klass < self
extension = Module.new extension = Module.new
klass.const_set(:RequestStoreWrapExtension, extension) klass.const_set(:RequestCacheExtension, extension)
klass.prepend(extension) klass.prepend(extension)
end end
...@@ -54,30 +54,26 @@ module Gitlab ...@@ -54,30 +54,26 @@ module Gitlab
end end
def request_store_wrap(method_name, &method_key_block) 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| define_method(method_name) do |*args|
store = store =
if RequestStore.active? if RequestStore.active?
RequestStore.store RequestStore.store
else else
ivar_name = # ! and ? cannot be used as ivar name 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_get(ivar_name) ||
instance_variable_set(ivar_name, {}) instance_variable_set(ivar_name, {})
end end
key = send("#{method_name}_cache_key", args) key = __send__(cache_key_method_name, args)
if store.key?(key) store.fetch(key) { store[key] = super(*args) }
store[key]
else
store[key] = super(*args)
end
end end
cache_key_method_name = "#{method_name}_cache_key"
define_method(cache_key_method_name) do |args| define_method(cache_key_method_name) do |args|
klass = self.class klass = self.class
......
module Gitlab module Gitlab
class UserAccess class UserAccess
extend Gitlab::Cache::RequestStoreWrap extend Gitlab::Cache::RequestCache
request_store_wrap_key do request_store_wrap_key do
[user&.id, project&.id] [user&.id, project&.id]
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Cache::RequestStoreWrap, :request_store do describe Gitlab::Cache::RequestCache, :request_store do
let(:klass) do let(:klass) do
Class.new do Class.new do
extend Gitlab::Cache::RequestStoreWrap extend Gitlab::Cache::RequestCache
attr_accessor :id, :name, :result, :extra attr_accessor :id, :name, :result, :extra
......
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