Skip to content

Commit 3ee0116

Browse files
committed
Optimize cache expansion by skipping rails cache id in nested keys.
1 parent 2c568f1 commit 3ee0116

File tree

2 files changed

+64
-58
lines changed

2 files changed

+64
-58
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -25,69 +25,75 @@ module Strategy
2525
autoload :LocalCache, 'active_support/cache/strategy/local_cache'
2626
end
2727

28-
# Creates a new CacheStore object according to the given options.
29-
#
30-
# If no arguments are passed to this method, then a new
31-
# ActiveSupport::Cache::MemoryStore object will be returned.
32-
#
33-
# If you pass a Symbol as the first argument, then a corresponding cache
34-
# store class under the ActiveSupport::Cache namespace will be created.
35-
# For example:
36-
#
37-
# ActiveSupport::Cache.lookup_store(:memory_store)
38-
# # => returns a new ActiveSupport::Cache::MemoryStore object
39-
#
40-
# ActiveSupport::Cache.lookup_store(:mem_cache_store)
41-
# # => returns a new ActiveSupport::Cache::MemCacheStore object
42-
#
43-
# Any additional arguments will be passed to the corresponding cache store
44-
# class's constructor:
45-
#
46-
# ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache")
47-
# # => same as: ActiveSupport::Cache::FileStore.new("/tmp/cache")
48-
#
49-
# If the first argument is not a Symbol, then it will simply be returned:
50-
#
51-
# ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
52-
# # => returns MyOwnCacheStore.new
53-
def self.lookup_store(*store_option)
54-
store, *parameters = *Array.wrap(store_option).flatten
55-
56-
case store
57-
when Symbol
58-
store_class_name = store.to_s.camelize
59-
store_class =
60-
begin
61-
require "active_support/cache/#{store}"
62-
rescue LoadError => e
63-
raise "Could not find cache store adapter for #{store} (#{e})"
64-
else
65-
ActiveSupport::Cache.const_get(store_class_name)
66-
end
67-
store_class.new(*parameters)
68-
when nil
69-
ActiveSupport::Cache::MemoryStore.new
70-
else
71-
store
28+
class << self
29+
# Creates a new CacheStore object according to the given options.
30+
#
31+
# If no arguments are passed to this method, then a new
32+
# ActiveSupport::Cache::MemoryStore object will be returned.
33+
#
34+
# If you pass a Symbol as the first argument, then a corresponding cache
35+
# store class under the ActiveSupport::Cache namespace will be created.
36+
# For example:
37+
#
38+
# ActiveSupport::Cache.lookup_store(:memory_store)
39+
# # => returns a new ActiveSupport::Cache::MemoryStore object
40+
#
41+
# ActiveSupport::Cache.lookup_store(:mem_cache_store)
42+
# # => returns a new ActiveSupport::Cache::MemCacheStore object
43+
#
44+
# Any additional arguments will be passed to the corresponding cache store
45+
# class's constructor:
46+
#
47+
# ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache")
48+
# # => same as: ActiveSupport::Cache::FileStore.new("/tmp/cache")
49+
#
50+
# If the first argument is not a Symbol, then it will simply be returned:
51+
#
52+
# ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
53+
# # => returns MyOwnCacheStore.new
54+
def lookup_store(*store_option)
55+
store, *parameters = *Array.wrap(store_option).flatten
56+
57+
case store
58+
when Symbol
59+
store_class_name = store.to_s.camelize
60+
store_class =
61+
begin
62+
require "active_support/cache/#{store}"
63+
rescue LoadError => e
64+
raise "Could not find cache store adapter for #{store} (#{e})"
65+
else
66+
ActiveSupport::Cache.const_get(store_class_name)
67+
end
68+
store_class.new(*parameters)
69+
when nil
70+
ActiveSupport::Cache::MemoryStore.new
71+
else
72+
store
73+
end
7274
end
73-
end
7475

75-
def self.expand_cache_key(key, namespace = nil)
76-
expanded_cache_key = namespace ? "#{namespace}/" : ""
76+
def expand_cache_key(key, namespace = nil)
77+
expanded_cache_key = namespace ? "#{namespace}/" : ""
78+
79+
prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
80+
if prefix
81+
expanded_cache_key << "#{prefix}/"
82+
end
7783

78-
prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
79-
if prefix
80-
expanded_cache_key << "#{prefix}/"
84+
expanded_cache_key << retrieve_cache_key(key)
85+
expanded_cache_key
8186
end
8287

83-
expanded_cache_key <<
88+
private
89+
90+
def retrieve_cache_key(key)
8491
case
8592
when key.respond_to?(:cache_key) then key.cache_key
86-
when key.is_a?(Array) then key.map { |element| expand_cache_key(element) }.to_param
93+
when key.is_a?(Array) then key.map { |element| retrieve_cache_key(element) }.to_param
8794
else key.to_param
8895
end.to_s
89-
90-
expanded_cache_key
96+
end
9197
end
9298

9399
# An abstract cache store class. There are multiple cache store

activesupport/test/caching_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def test_expand_cache_key_with_rails_cache_id
1212
begin
1313
ENV['RAILS_CACHE_ID'] = 'c99'
1414
assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key(:foo)
15-
assert_equal 'c99/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
16-
assert_equal 'c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar])
15+
assert_equal 'c99/foo', ActiveSupport::Cache.expand_cache_key([:foo])
16+
assert_equal 'c99/foo/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar])
1717
assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key(:foo, :nm)
18-
assert_equal 'nm/c99/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
19-
assert_equal 'nm/c99/c99/foo/c99/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm)
18+
assert_equal 'nm/c99/foo', ActiveSupport::Cache.expand_cache_key([:foo], :nm)
19+
assert_equal 'nm/c99/foo/bar', ActiveSupport::Cache.expand_cache_key([:foo, :bar], :nm)
2020
ensure
2121
ENV['RAILS_CACHE_ID'] = nil
2222
end

0 commit comments

Comments
 (0)