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

Conversation

bogdan
Copy link
Contributor

@bogdan bogdan commented Nov 6, 2017

Summary

Remove code duplication in the implementation of the cache key

@rails-bot
Copy link

r? @georgeclaghorn

(@rails-bot has picked a reviewer for you, use r? to override)

@@ -57,7 +57,7 @@ def view_cache_dependencies
# Convenience accessor.
def cache(key, options = {}, &block) # :doc:
if cache_configured?
cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
cache_store.fetch([:controller, key], options, &block)
Copy link
Member

Choose a reason for hiding this comment

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

This sounds like a behavioural change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope: in previous implementation controller as namespace prepends the key just in the same way as if the key has two parts in current implementation.

Copy link
Member

Choose a reason for hiding this comment

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

What about the ENV value?


expanded_cache_key << retrieve_cache_key(key)
expanded_cache_key
[namespace, ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"], retrieve_cache_key(key)].compact.join("/")
Copy link
Member

Choose a reason for hiding this comment

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

This is more allocations in a reasonably hot path, and also seems less readable to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is less allocation: in previous impelementation you allocate many strings. In this implementation you allocate one array and the dup of it (via compact).
We can change compact to tap(&:compact!) if you care about allocations.

Put 👎 and I will revert to previous implementation. As I am not super concerned.

I am not sure what is "a reasonably hot path". Can you explain?

Copy link
Member

Choose a reason for hiding this comment

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

Previously it was one string in expanded_cache_key (two if namespace), plus one more if prefix. With this change it's the array literal, the compacted array, and the joined string.

"hot path" = code-path that's called very frequently, deserving of performance concern; reasonably (in this context) = not very, but somewhat [hot].

# 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants