-
15-10-2019 - |
题
我不太确定如何确保缓存工作,但我敢肯定不是。我有一个带有索引操作的用户控制器,该操作是我的操作缓存,直到创建新用户为止。这是代码:
UsersController < ApplicationController
caches_action :index
def index
@users = User.all
end
def create
expires_action :index
...
end
end
现在,当我访问我的日志中 index
行动,我看到:
Cached fragment hit: views/localhost:3000/users (0.0ms)
Filter chain halted as [#<ActionController::Filters::AroundFilter:0xe2fbd3 @identifier=nil, @kind=:filter, @options={:only=>#<Set: {"index", "new"}>, :if=>nil, :unless=>nil}, @method=#<Proc:0x186cb11@/Users/bradrobertson/.rvm/gems/jruby-1.5.3/gems/actionpack-2.3.10/lib/action_controller/caching/actions.rb:64>>] did_not_yield.
我不确定什么 filter chain halted ... did_not_yield
全部都是关于的,我也看到了 select * from users
...每次都被调用,这不是我所期望的。
有人可以启发我,了解这里发生了什么,为什么这不会像我期望的那样行事? IE。为什么当整个动作的输出被缓存时,所有用户都会运行?
解决方案
这 filter chain halted
消息意味着有一个周围的过滤器可以阻止操作调用。这可能是动作缓存,它阻止了实际动作的发生。它没 屈服 对动作,因为它在缓存中找到了某些内容,如上面的消息所建议。
这 User.all
根本不应该运行,因为它是在操作中,但是在过滤器之前进行的任何操作。如果您的页面在某种形式的身份验证后面,则比身份验证检查可能触发了SQL调用。因此,您可能需要仔细检查SQL日志的真正来自哪里。
此外,适当的语法(至少根据铁轨指南)的到期:
expire_action :action => :index
更多信息: 铁轨指南
不隶属于 StackOverflow