Created
October 26, 2012 14:21
-
-
Save bsodmike/3959098 to your computer and use it in GitHub Desktop.
Caching in Rails 3.2.8 including Cache Sweeping with Redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set default cache store as redis | |
config.cache_store = :redis_store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.action_controller.perform_caching = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListingsController < ApplicationController | |
cache_sweeper :fragment_sweeper | |
#... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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