Easily build a great vocabulary without studying! Now on the App Store!
Click here for more info about the app

Rails: Expiring a cached page with namespaces and sweepers


I've got some pages that are cached using their permalinks on the filesystem, such as http://example.com/about-us.html which will need to map to RAILS_ROOT/public/about-us.html ... The issue I have is that I use a namespace for the admin area and the controllers in the namespace are responsible for expiring the cached pages, i.e., when the resources are updated by an admin.

Check out Rails Envy for a great tutorial for getting page caching set up: http://www.railsenvy.com/2007/2/28/rails-caching-tutorial

So what I want to do is expire the pages from inside my namespace. To accomplish this I need to use the pages route in my sweeper class.

class PageSweeper < ActionController::Caching::Sweeper
  #... after_save, after_destroy... we'll exprire the cache
  def expire_cache_for(record)
    #permalink rather than record id like /213.html

Since I'm using a permalink to cache the page, I need to expire it with the permalink too.

Tagged w/ #caching #production #rails #ruby #scaling #sweeperruby on rails