Taggable cache <img src=“https://secure.travis-ci.org/brain-geek/taggable-cache.png”/>

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.

How to use

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

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 both supported. Tested with ruby 1.9.3. Cache storage engines tested:

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 © 2012 Alex Rozumey. See LICENSE.txt for further details.