2
以下是設置。我有一個基本上是簡單CMS的網站。這裏是路線文件:根URL無效的動作緩存
map.connect ':url', :controller => :pages, :action => :show
map.root :controller => :pages, :action => :show, :url => "/"
頁面控制器是這樣的:
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
所以,當訪問者點擊 「/」 它映射到:控制器=> 「頁面,:動作=>」 秀,:url =>「/」。這會在第一次嘗試時生成一個緩存版本,然後在那裏返回適當的結果。日誌文件顯示:
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:[email protected]/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:[email protected]/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/]
確定 - 迄今爲止都很好。當我更新頁面時,應該使緩存過期(參見上文)。日誌顯示:
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]
看到問題了嗎? Rails將清除名爲「index」的緩存,但將其設置爲「/」。當然這會導致緩存不被清除,所以訪問者現在正在看到舊版本。
我切換到片段緩存,所以這不再是一個問題,但我仍然懷疑在這裏的Rails中的錯誤。 – askegg 2009-08-04 01:36:26