Blog

Monday, October 18, 2010

Cached Key for ActiveRecord Collections

Last night I was doing some optimizations on Heroku utilizing Vanish, their reverse proxy server, and wanted to set an etag for an ActiveRecord collection. Basically I wanted to do something like this:

def index
    @videos = Video.published

    fresh_when :etag => @videos
    headers['Cache-Control'] = 'public'
   end

Surprisingly an ActiveRecord collection does not support the cache_key. Fortunately Jim Gay has written a gem, group_cache_key, to resolve this shortcoming. Just add the gem to your Gemfile and bundle install.

Please note this blog is no longer maintained. Please visit CivilCode Inc - Custom Software Development.