Taggable cache

This gem simplifies cache expiration in rails by adding depends_on parameter to rails cache setting, which makes cache element expire when depending object is changed.

Build Status

How to use

Taggable-cache uses redis to store cache tags data. You can use anything for an actual cache storage.

Controller(action caching):

caches_action :index

def index
  Page.load_lot_of_data

  #this depends on any Page object change - creation, deletion, update
  depends_on Page
end

View(fragment caching):

<%# Page.active can be AR scope, every Page object on change is checked if it is in this scope, and if is - expires cache element %>
<% cache 'cache_entry', :depends_on => Page.active do %>
  <% Page.load_lot_of_data %>
<% end %>

Usage with Rails.cache:

page = Page.first
#this cache key expires only when this Page object is changed/deleted
Rails.cache.write('key', 'value', :depends_on => page)

Usage with cells gem:

class TabsCell < Cell::Rails
  cache :index, nil, :depends_on => [Category, Metacategory, Product]

  def index
    render
  end
end

If you are using redis for cache, it is recommended to set default expire_ttl value:

config.cache_store = ActiveSupport::Cache::RedisStore.new(:expire_in => 16.hours.to_i)

What is supported

Rails 3.1.x and 3.2.x are supported. Tested with ruby 1.9.3, but should work on 1.8.7. As cache storage you can use redis, memcached and file store caches. Other cache storages should work but have not been tested by author.

Customizing store backend options

You can set custom host/port or store in taggable:

TaggableCache.settings = {
  :store => :redis, 
  :host => '127.0.0.1', 
  :port => 6379
}

Contributing to taggable-cache

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.

  • Fork the project.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2012 Alex Rozumey. See LICENSE.txt for further details.