Taggable cache simplifies rails cache expiration
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Note: it DOES NOT work with rails 4.x . If you want to have similar functionality in your rails 4.x project - please use github.com/brain-geek/activerecord_cashier

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.

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

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

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

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 rubies 1.9.3 and 2.0.0. 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 => '', 
  :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.