Skip to content

Instantly share code, notes, and snippets.

@Martin91
Forked from parndt/gist:1011435
Last active August 29, 2015 14:02
Show Gist options
  • Save Martin91/d75c65e9d73f1d1fb907 to your computer and use it in GitHub Desktop.
Save Martin91/d75c65e9d73f1d1fb907 to your computer and use it in GitHub Desktop.
How to cache refinery pages
# Rails4 full page caching is removed and extracted as gem, details from Rails Guides:
# Rails 4.0 has removed Action and Page caching from Action Pack.
# You will need to add the actionpack-action_caching gem in order to use caches_action
# and the actionpack-page_caching to use caches_pages in your controllers.
gem 'actionpack-page_caching'
# put in config/application.rb
config.to_prepare do
Refinery::PagesController.module_eval do
caches_page :show, :unless => proc {|c| c.refinery_user_signed_in? || c.flash.any? }
caches_page :home, :unless => proc {|c| c.refinery_user_signed_in? || c.flash.any? }
end
Refinery::Page.module_eval do
after_save :clear_static_caching!
after_destroy :clear_static_caching!
def clear_static_caching!
Refinery::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
end
@rainchen
Copy link

rainchen commented Jun 3, 2014

to simplify clear_static_caching! , you can try sth like ApplicationController.expire_page(self.url)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment