Here's the setup. I have web site which is essentially a simple CMS. Here is the routes file:
map.connect ':url', :controller => :pages, :action => :show
map.root :controller => :pages, :action => :show, :url => "/"
The page controller is thus:
class PagesController < ApplicationController
before_filter :verify_access, :except => [:show]
# Cache show action if we are not logged in.
caches_action :show, :layout => false, :unless => Proc.new { |controller| controller.logged_in? }
def update
@page = Page.find(params[:id])
respond_to do |format|
expire_action :action => :show, :url => @page.url
So when a visitor hits "/" it maps to :controller = "pages, :action = "show, :url = "/". This generates a cached version on first try, then returns the appropriate result there after. The log files show:
Processing PagesController#show (for 127.0.0.1 at 2009-08-02 14:15:01) [GET]
Parameters: {"action"=>"show", "url"=>"/", "controller"=>"pages"}
Cached fragment hit: views/out.local// (0.1ms)
Rendering template within layouts/application
Filter chain halted as [#<ActionController::Filters::AroundFilter:0x23eb03c @identifier=nil, @method=#<Proc:0x01904858@/Library/Ruby/Gems/1.8/gems/actionpack-2.3.3/lib/action_controller/caching/actions.rb:64>, @kind=:filter, @options={:only=>#<Set: {"show"}>, :if=>nil, :unless=>#<Proc:0x025137ac@/Users/askegg/Sites/out/app/controllers/pages_controller.rb:6>}>] did_not_yield.
Completed in 2ms (View: 1, DB: 0) | 200 OK [http://out.local/]
OK - all good so far. When I update the page, it should expire the cache (see above). The logs show:
Page Load (0.2ms) SELECT * FROM "pages" WHERE ("pages"."id" = 3)
Page Load (0.1ms) SELECT "pages".id FROM "pages" WHERE ("pages"."url" = '/' AND "pages".domain_id = 1 AND "pages".id <> 3) LIMIT 1
Expired fragment: views/out.local/index (0.1ms)
Redirected to http://out.local/pages/3
Completed in 9ms (DB: 0) | 302 Found [http://out.local/pages/3]
See the problem? Rails is clearing the cache named "index", but it sets it as "/". Naturally this results in the cache NOT being cleared, so visitors are now seeing the old version.