根URL的操作缓存不起作用

时间:2009-08-02 04:22:27

标签: ruby-on-rails caching

这是设置。我有一个网站,它本质上是一个简单的CMS。这是路线文件:

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

因此,当访问者点击“/”时,它会映射到:controller =&gt; “pages,:action =&gt;”show,:url =&gt; “/”。这会在第一次尝试时生成缓存版本,然后在之后返回相应的结果。日志文件显示:

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/]
好的 - 到目前为止一切都很好。当我更新页面时,它应该使缓存失效(见上文)。日志显示:

  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”的缓存,但它将其设置为“/”。当然,这会导致缓存未被清除,因此访问者现在可以看到旧版本。

1 个答案:

答案 0 :(得分:0)

我没有使用动作缓存,但是我知道如果我缓存诸如“/ posts / index”之类的页面而使用页面缓存,并且我也将其设为root_url,Rails会将它缓存为:

RAILS_ROOT/public/index.html
RAILS_ROOT/public/posts/index.html

要使他们两个到期,我必须在清道夫中完成:

expire_page :controller => 'posts', :action => 'index'
expire_page '/index'

所以也许expire_action也会采用字符串路径arg?