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

Caching collections with etags #90

Open
manuelmeurer opened this issue Feb 16, 2014 · 3 comments
Open

Caching collections with etags #90

manuelmeurer opened this issue Feb 16, 2014 · 3 comments
Milestone

Comments

@manuelmeurer
Copy link
Contributor

Right now RocketPants only supports caching collections using "expires in". I would like to add the option to cache collections with etags. There have been several approaches:

I am testing this right now by calling

cache_response collection, true if RocketPants.caching_enabled?

in my controller (I am using Rabl instead of Active Record Serializers, so I don't use exposes). As you see, I am passing my collection and indicate that it's a singular resource, thus triggering the etag caching. Then I use the following to monkeypatch ActiveRecord::Relation to expose a cache_key and rp_object_key:

# config/initializers/ar_relation_cache_key.rb
ActiveRecord::Relation.class_eval do
  def cache_key
    parts = [self.to_sql]
    if self.any?
      parts.concat [
        self.count,
        self.pluck(:id).join('-'),
        self.maximum(:updated_at).to_s(:number),
        self.maximum(:created_at).to_s(:number)
      ]
    end
    parts.join('/')
  end

  def rp_object_key
    self.to_sql
  end
end

Would you be open to adding an option where RocketPants would check the collection for a cache_key, use etag caching if it has one and "expires in" if not?

@Sutto
Copy link
Owner

Sutto commented Feb 25, 2014

Definitely open to it, but I don't have time to work on it - would you perhaps be interested in implementing it and sending a PR?

@manuelmeurer
Copy link
Contributor Author

Sure, I will work on it soon!

@Sutto
Copy link
Owner

Sutto commented Mar 2, 2014

Cheers!

@Sutto Sutto added this to the v1.13 milestone May 25, 2015
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

No branches or pull requests

2 participants