Created
October 16, 2011 02:27
-
-
Save aaronvb/1290431 to your computer and use it in GitHub Desktop.
pagination page cache sweeper
This file contains 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 ArticleSweeper < ActionController::Caching::Sweeper | |
observe Article # This sweeper is going to keep an eye on the article model | |
# If our sweeper detects that a article was created call this | |
def after_create(article) | |
expire_cache_for(article) | |
end | |
# If our sweeper detects that a article was updated call this | |
def after_update(article) | |
expire_cache_for(article) | |
expire_cache_for_single(article) | |
end | |
# If our sweeper detects that a article was deleted call this | |
def after_destroy(article) | |
expire_cache_for(article) | |
expire_cache_for_single(article) | |
end | |
private | |
def expire_cache_for_single(article) | |
expire_page(:controller => 'articles', :action => 'show') | |
end | |
def expire_cache_for(article) | |
# Expire the index page now that we added or modified an article | |
expire_page(:controller => 'articles', :action => 'index') | |
# expire the index.html which is created by root_path | |
expire_page '/index.html' | |
# manually remove pages(1.html, 2.html, etc) | |
if File.exist?("#{Rails.root}/public/articles/page") # check to make sure directory exists | |
Dir.foreach("#{Rails.root}/public/articles/page") do |entry| | |
unless entry == "." || entry == ".." || entry == ".gitignore" # ignore dot files | |
File.delete("#{Rails.root}/public/articles/page/#{entry}") | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment