Skip to content

Instantly share code, notes, and snippets.

@thomasmaas
Created March 12, 2011 00:30
Show Gist options
  • Save thomasmaas/866861 to your computer and use it in GitHub Desktop.
Save thomasmaas/866861 to your computer and use it in GitHub Desktop.
Fragment caching the refinerycms menu to speed things up dramatically. On Heroku, make sure to use memcached store if you use more than 1 dyno.
# views/layouts/shared/_menu.html.erb
hide_children = RefinerySetting.find_or_set(:menu_hide_children, false) if hide_children.nil?
+ cache('site_menu') do
collection ||= @menu_pages.includes(:slugs)
</nav>
+ end
# create sweepers/page_sweeper.rb
class PageSweeper < ActionController::Caching::Sweeper
observe Page
def after_create(page)
expire_cache_for(page)
end
def after_update(page)
expire_cache_for(page)
end
def after_destroy(page)
expire_cache_for(page)
end
private
def expire_cache_for(page)
expire_fragment('site_menu')
end
end
# in controllers/admin/pages_controller.rb
module Admin
class PagesController < Admin::BaseController
+ cache_sweeper :page_sweeper
# OR if you're not comfortable overriding AdminPagesController inside the actual file: do it in config/application.rb
class Application < Rails::Application
+ config.to_prepare do
+ ::Admin::PagesController.module_eval %{
+ cache_sweeper :page_sweeper
+ }
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment