aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/cache.rb
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2017-11-06 11:08:12 +0200
committerBogdan Gusiev <agresso@gmail.com>2017-11-09 17:12:41 +0200
commit57f0e3d1300d01444d2a311560c055d26968dc3f (patch)
tree63b52362c4acecd24f7c892b21fed659ff0fd73d /activesupport/lib/active_support/cache.rb
parent2d2aa0ee4b1466e79b1c9b5c8c8fddb8f54ada3d (diff)
downloadrails-57f0e3d1300d01444d2a311560c055d26968dc3f.tar.gz
rails-57f0e3d1300d01444d2a311560c055d26968dc3f.tar.bz2
rails-57f0e3d1300d01444d2a311560c055d26968dc3f.zip
Remove code duplication in ActiveSupport::Cache
Diffstat (limited to 'activesupport/lib/active_support/cache.rb')
-rw-r--r--activesupport/lib/active_support/cache.rb37
1 files changed, 11 insertions, 26 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 976104e0c4..9f91f9b576 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -90,11 +90,16 @@ module ActiveSupport
private
def retrieve_cache_key(key)
case
- when key.respond_to?(:cache_key_with_version) then key.cache_key_with_version
- when key.respond_to?(:cache_key) then key.cache_key
- when key.is_a?(Array) then key.map { |element| retrieve_cache_key(element) }.to_param
- when key.respond_to?(:to_a) then retrieve_cache_key(key.to_a)
- else key.to_param
+ when key.respond_to?(:cache_key_with_version)
+ key.cache_key_with_version
+ when key.respond_to?(:cache_key)
+ key.cache_key
+ when key.is_a?(Hash)
+ key.sort_by { |k, _| k.to_s }.collect { |k, v| "#{k}=#{v}" }.to_param
+ when key.respond_to?(:to_a)
+ key.to_a.collect { |element| retrieve_cache_key(element) }.to_param
+ else
+ key.to_param
end.to_s
end
@@ -565,33 +570,13 @@ module ActiveSupport
# Prefixes a key with the namespace. Namespace and key will be delimited
# with a colon.
def normalize_key(key, options)
- key = expanded_key(key)
+ key = Cache.expand_cache_key(key)
namespace = options[:namespace] if options
prefix = namespace.is_a?(Proc) ? namespace.call : namespace
key = "#{prefix}:#{key}" if prefix
key
end
- # Expands key to be a consistent string value. Invokes +cache_key+ if
- # object responds to +cache_key+. Otherwise, +to_param+ method will be
- # called. If the key is a Hash, then keys will be sorted alphabetically.
- def expanded_key(key)
- return key.cache_key.to_s if key.respond_to?(:cache_key)
-
- case key
- when Array
- if key.size > 1
- key = key.collect { |element| expanded_key(element) }
- else
- key = key.first
- end
- when Hash
- key = key.sort_by { |k, _| k.to_s }.collect { |k, v| "#{k}=#{v}" }
- end
-
- key.to_param
- end
-
def normalize_version(key, options = nil)
(options && options[:version].try(:to_param)) || expanded_version(key)
end