aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorAndrew White <pixeltrix@users.noreply.github.com>2017-11-10 11:11:08 +0000
committerGitHub <noreply@github.com>2017-11-10 11:11:08 +0000
commit1680c0e5790b1ea6effcb7e1dd93245f247d8bfa (patch)
treeee65e7450462e78df3f1fd9bbaec2fdb5e86a892 /activesupport
parent179f2d75d1b9c244565d4bfbec76e93c495fa89b (diff)
parent57f0e3d1300d01444d2a311560c055d26968dc3f (diff)
downloadrails-1680c0e5790b1ea6effcb7e1dd93245f247d8bfa.tar.gz
rails-1680c0e5790b1ea6effcb7e1dd93245f247d8bfa.tar.bz2
rails-1680c0e5790b1ea6effcb7e1dd93245f247d8bfa.zip
Merge pull request #31065 from bogdan/cleanup-cache-key
Remove code duplication in ActiveSupport::Cache
Diffstat (limited to 'activesupport')
-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