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

Fragment Cache #802

Closed
joaomdmoura opened this issue Feb 2, 2015 · 13 comments
Closed

Fragment Cache #802

joaomdmoura opened this issue Feb 2, 2015 · 13 comments

Comments

@joaomdmoura
Copy link
Member

Considering my PR #693
Cache Composition (http://robots.thoughtbot.com/fast-json-apis-in-rails-with-key-based-caches-and) is one of the work-arounds for the absence of fragmente cache on AMS.

As we know, a fragment cache implementation would be extremely efficient to improve API's that have some dynamic attributes that may vary on every request.

Assuming that PR #693 will be merged soon, this should be a new feature to the cache implementation, my proposal is to follow Rails conventions, adding two new options to cache method: only and except.

Example:

class PostSerializer < ActiveModel::Serializer
  cache key: 'post', expires_in: 3.hours, only: [:title]
  attributes :title, :body

  has_many :comments

  url :post
end

In the example, only the title attribute would be cached, everything else would be retrieved from the Post object. Except would work exactly the opposite.

Despite of being a simple new feature it isn't quite easy and small. So I'm not moving forward with this on PR #693

I'll try to work on this asap, but would like to hear some thoughts about it.
cc/ @kurko @steveklabnik @nwjsmith

@guilleiguaran
Copy link
Member

I just want to say that if this is done active_model_serializers will be shipped by default in new Rails 5.0 apps.

@joaomdmoura
Copy link
Member Author

@guilleiguaran haha 👍 it will be done!

@kurko
Copy link
Member

kurko commented Feb 2, 2015

Go go go, @joaomdmoura 😁

@antonioparisi
Copy link

👍

@joaomdmoura
Copy link
Member Author

I'm not sure if I should also enable users to cache relationships (all their attributes). @kurko any thoughts?
Btw, the core is already working :) working

@joaomdmoura
Copy link
Member Author

@kurko, never mind haha I ended up doing it.
So now you can also specify only or except with associations names, it will fragment cache it.

Example.

LikeSerializer = Class.new(ActiveModel::Serializer) do
  cache only: [:post]
  attributes :id, :time

  belongs_to :post
end

The response generated by the post relation will be cached, but it won't cache the post object. So, your PostSerializer will not use this cache because it may not contain exactly what you want.

I'll finish some details, squash, document it and open the PR.

@antonioparisi
Copy link

Excellent work! @joaomdmoura

On Wednesday, 11 February 2015, João Moura [email protected] wrote:

@kurko https://github.com/kurko, never mind haha I ended up doing it.
So now you can also specify only or except with associations names, it
will fragment cache it.

ex.

LikeSerializer = Class.new(ActiveModel::Serializer) do
cache only: [:post]
attributes :id, :time

belongs_to :postend

The post part of the response will be cached, but it won't cache the post
object. So, your PostSerializer will not use this cache because it may
not contain exactly what you want.

Will squash, document it and open the PR probably tomorrow.


Reply to this email directly or view it on GitHub
#802 (comment)
.

Sent from Gmail Mobile

@kurko
Copy link
Member

kurko commented Feb 11, 2015

Promising!

@gaurish
Copy link

gaurish commented Feb 27, 2015

Hey, how are things going with this? do you need any help?

@joaomdmoura
Copy link
Member Author

@gaurish just finishing actually 😄 Yesterday I've made the final adjustments that @kurko pointed out. I'll update the PR this weekend.

@joshsmith
Copy link
Contributor

Hey @joaomdmoura this is awesome to hear. Looking forward to seeing the updates!

@kenips
Copy link
Contributor

kenips commented Mar 11, 2015

thanks so much for your work @joaomdmoura 🍻

@joaomdmoura
Copy link
Member Author

it was already merged. I'm closing it ❤️

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

No branches or pull requests

7 participants