Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove code duplication in ActiveSupport::Cache #31065

Merged
merged 1 commit into from
Nov 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 11 additions & 26 deletions activesupport/lib/active_support/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ def expand_cache_key(key, namespace = nil)
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

Expand Down Expand Up @@ -565,33 +570,13 @@ def merged_options(call_options)
# 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there relevant history to why this method wasn't taught about cache_key_with_version?

Copy link
Contributor Author

@bogdan bogdan Nov 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache_key_with_version was added by @dhh not so long ago. I was in discussion on that feature and we didn't figure out this code dup at that moment. It has no 100% test coverage so far.


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
Expand Down