Created
January 31, 2012 23:04
-
-
Save parndt/1713646 to your computer and use it in GitHub Desktop.
Rudimentary page caching.
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
::PagesController.module_eval do | |
caches_page :show, :unless => proc {|c| c.user_signed_in? || c.flash.any? } | |
caches_page :home, :unless => proc {|c| c.user_signed_in? || c.flash.any? } | |
end | |
::Page.module_eval do | |
after_save :clear_static_caching! | |
after_destroy :clear_static_caching! | |
def clear_static_caching! | |
Page.all.map(&:url).map{|u| | |
[(u if u.is_a?(String)), (u[:path] if u.respond_to?(:keys))].compact | |
}.flatten.map{ |u| [(u.split('/').last || 'index'), 'html'].join('.')}.each do |page| | |
if (static_file = Rails.root.join('public', page)).file? | |
$stdout.puts "Clearing cached page #{static_file.split.last}" | |
static_file.delete | |
end | |
end | |
end | |
protected :clear_static_caching! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
caches_page :show, :if => proc {|c| c.request.query_parameters.length == 0}