Skip to content

Instantly share code, notes, and snippets.

@bsodmike
Created October 26, 2012 14:21
Show Gist options
  • Save bsodmike/3959098 to your computer and use it in GitHub Desktop.
Save bsodmike/3959098 to your computer and use it in GitHub Desktop.
Caching in Rails 3.2.8 including Cache Sweeping with Redis
# set default cache store as redis
config.cache_store = :redis_store
class FragmentSweeper < ActionController::Caching::Sweeper
observe Listing
def after_create(record)
expire_cache_for(record)
end
def after_save(record)
expire_cache_for(record)
end
def after_update(record)
expire_cache_for(record)
end
def after_destroy(record)
expire_cache_for(record)
end
private
def expire_cache_for(record)
@controller ||= ActionController::Base.new
expire_fragment "listings/#{record.id}-#{record.updated_at.to_i}"
Rails.logger.debug "***\n>>> Listing #{record.id} cache fragment expired!\n***" if Rails.env.development?
end
end
config.action_controller.perform_caching = true
class ListingsController < ApplicationController
cache_sweeper :fragment_sweeper
#...
end
<%= cache @listing do %>
<p>Content</p>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment