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

Use ActiveSupport::Cache.expand_cache_key for cache key expansions #1878

Merged
merged 3 commits into from
Aug 13, 2016

Conversation

markiz
Copy link
Contributor

@markiz markiz commented Aug 12, 2016

Purpose

Allows using non-primitive objects as object cache key.
For example, we might want to return an array of objects that define #cache_key as our serialized object's #cache_key

Changes

join("/") changed to ActiveSupport::Cache.expand_cache_key

Caveats

Dependency on ActiveSupport (which we already have)

Additional helpful information

The test is a bit contrived, sorry about that.
#expand_cache_key can be redefined to change the actual implementation.

Previous version of AMS did use ActiveSupport::Cache.expand_cache_key

@mention-bot
Copy link

@markiz, thanks for your PR! By analyzing the annotation information on this pull request, we identified @bf4, @kevintyll and @beauby to be potential reviewers

@markiz markiz force-pushed the add-expand-cache-key branch from 2811e9e to 04e08a0 Compare August 12, 2016 10:14
@NullVoxPopuli
Copy link
Contributor

what are the benefits of this? / what's the use case?

@markiz
Copy link
Contributor Author

markiz commented Aug 12, 2016

@NullVoxPopuli

We use non-primitive object as cache keys, another contrived example:

class User
  def cache_key
    [super, latest_post, latest_newsfeed_item, latest_friend_request] # where latest_post, latest_newsfeed_item, latest_friend_request are ActiveRecord models, possibly nil
  end
end

There wouldn't be any problem if AMS didn't try to join those.
In 0.8 it did the right thing with expand_cache_key
We can probably live with overridable expand_cache_key too (and override it to do what we want). But using join here breaks compat for us.

@bf4
Copy link
Member

bf4 commented Aug 12, 2016

@markiz lgtm,

  1. would you mind adding a change log?
  2. would you mind looking at the object_cache_key methods in the Caching mixin?

@markiz
Copy link
Contributor Author

markiz commented Aug 12, 2016

@bf4:

  1. Sure (4ce7dc0)
  2. I'm not sure I follow. object_cache_key duplicates some of the functionality in the expand_cache_key, but it also has some differing behavior, so I don't see any immediate need to change it.

@NullVoxPopuli
Copy link
Contributor

@markiz thanks for that. I didn't know this was an old feature, and I had no idea people even did this. Certainly looks looks better than building your own cache key string. :-)

@bf4
Copy link
Member

bf4 commented Aug 12, 2016

@NullVoxPopuli yeah.. I'm not sure why it was removed. We use ActiveSupport::Cache which uses expand_cache_key but ¯_(ツ)_/¯

[
DerivedCacheKey.new(self, :name),
DerivedCacheKey.new(self, :id)
]
Copy link
Member

@bf4 bf4 Aug 12, 2016

Choose a reason for hiding this comment

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

@markiz my only real concern is that these classes are really weird-looking. Would you mind adding some comments why you designed it this way? Be nice to future devs :)

Copy link
Member

Choose a reason for hiding this comment

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

@markiz markiz force-pushed the add-expand-cache-key branch from 7b6302a to d0e54da Compare August 13, 2016 04:49
@bf4 bf4 merged commit 5f3bdcc into rails-api:master Aug 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants