Last active
December 10, 2015 07:18
-
-
Save bostonaholic/4400728 to your computer and use it in GitHub Desktop.
Rails 3 Action Sweeper on Heroku
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.active_record.observers = :post_sweeper |
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 ApplicationController < ActionController::Base | |
protect_from_forgery | |
cache_sweeper :post_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
gem 'memcachier' | |
gem 'dalli' |
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 PostSweeper < ActionController::Caching::Sweeper | |
observe Post | |
def after_save(post) | |
expire_cache_for_post_index | |
end | |
private | |
def expire_cache_for_post_index | |
cache_key = "views/#{request.host_with_port}/posts" | |
Rails.cache.delete(cache_key) | |
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
class PostsController < ApplicationController | |
caches_action :index | |
def index | |
@posts = Post.published | |
end | |
def show | |
@post = Post.find(params[:id]) | |
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.cache_store = :dalli_store |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment