-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Cache Support at AMS 0.10.0 #693
Conversation
👍 👍 👍 |
👍 |
It's awesome to bring cache back, Thank you. I read your idea in New Cache Support on AMS, and found it resolve the problem to reuse cache between index and show actions. I have use AMS on some projects and find two truth:
In fact, I have try to design a new cache strategy, and benchmark result always >50x faster than no cache. here is my demo app for benchmark and here is my Cache Strategy base AMS 0.9 I hope it's useful for you to design the new cache strategy for AMS 0.10 |
Hey @zlx, I have just checked your cache suggestion at 0.9.0. Btw, thank you for contributing! Yes, this implementation focus on make the cached objects as reusable as possible, it cache the serializer output, each major object individually, and does not expire it unless you tell it to. (Using I'm always hungry for new ideas and suggestions so let's keep improving it even after merged 😄 |
Hi, @joaomdmoura I find in real world, sometimes in one AMS object, we want to not cache some attributes, eg: is_follow like twitter, so we can make cache live more long time. consider add this support? thus the we can just use AMS for cache API. |
Hi @zlx yeah, totally agree. I'll work on it :) |
Awesome. look forward your great job |
on a pattern ```"#{key}-#{object.id}"```. | ||
|
||
**[NOTE] Every object is individually cached.** | ||
**[NOTE] The cache isn't automaticly expired after update an object.** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo, automaticly
.
@joaomdmoura can you rebase? Summoning @steveklabnik. Is this something you want for AMS right now? |
In general, caching is something we want in AMS, yes 😄 |
@kurko Of course, I might add some more commit too. I'll do this tonight 😄 |
Hey, has this been actively worked on lately? If caching is required for some apps, would 0.8 be a better fit? |
|
||
```ruby | ||
class PostSerializer < ActiveModel::Serializer | ||
cache key: 'post', expires_in: 3.hours |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joaomdmoura could we make key
optional? Then we could use object.cache_key
by default. Makes sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kurko I'm not sure if I have already done it 😕 But I'll check it.
Could you rebase this? We want to move forward with caching 😁 |
haha Actually I already started it 3 times 😞 but the tests always end up broken. |
I'm so sorry :( Getting this in is my number 1 priority now, I promise. Could you also squash your commits into one with a nice title and description? |
be1516b
to
ed8353d
Compare
@kurko It's done! Rebased, working and squashed. 😄 |
klass = serializer.class | ||
if klass._cache | ||
_cache_key = klass._cache_key || serializer.object.class.name.downcase | ||
klass._cache.fetch("#{_cache_key}-#{serializer.object.id}", klass._cache_options) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I wasn't clear in my previous comment (https://github.com/rails-api/active_model_serializers/pull/693/files#diff-04c6e90faac2675aa89e2176d2eec7d8R252). Basically, I believe we should use model.cache_key
here when key is not used (docs in http://api.rubyonrails.org/classes/ActiveRecord/Integration.html). If you don't specify a key, it'd use object.cache_key
:
Person.find(5).cache_key # => "people/5-20071224150000"
The problem with using only an id here is that if we update the model, it'll reuse an old cache because update_at
is disregarded. With cache_key
, it'll regard update_at
. With a custom key
, I believe updated_at
should also be included in this identifier.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@kurko totally agree. I'll work on this. |
@kurko there is a down side on adding the updated_at to the key that I would like to discuss. On the later versions of cache on AMS there wasn't a way to expire an specific cache on an abstracted logic, because there was no way to know the cache key. Do you think there is no need to worry about it? Or do you see some workaround? |
|
@kurko yeah, u're probably right. |
@@ -1,3 +1,4 @@ | |||
require 'pry' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A wild pry
appears
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Observe, pry
in its natural habitat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hahahahahah sorry people, already removed it. 😄
Is there a way for this cache-ing scheme to support something like Fragment/Russian Doll Caching? If not, this leaves AMS at a serious disadvantage to JBuilder for higher-performance apps. One of the things that is nice about the old |
Awesome! Smooth ⛵ |
YAY |
❤️ ❤️ ❤️ @kurko can we get a 0.10.0.alpha released soon? |
@kenips It's already WIP. I'll try to get it done next week. |
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
It's an upgrade based on the new Cache implementation rails-api#693. It allows to use the Rails conventions to cache specific attributes or associations. It's based on the Cache Composition implementation.
This is a implementation of cache support to the new version of AMS.
It started originally at the mailing list:
AMS: Caching with Matthijs Langenberg.
New Cache Support on AMS. with me
This implementation proposes x different approaches of the old versions of AMS.
ActiveSupport::Cache::Store
, plus akey
option that will be the prefix of the object cache on a pattern"#{key}-#{object.id}"
.There are many benefits on this approach:
ActiveSupport::Cache::Store
(:expires_in for example)cache_key
method.