【问题标题】:Rails action caching questionRails 动作缓存问题
【发布时间】:2011-06-03 11:45:09
【问题描述】:

我不太确定如何确保我的缓存正常工作,但我很确定它不是。我有一个带有索引操作的用户控制器,在创建新用户之前我一直在缓存它。代码如下:

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。当整个操作的输出应该被缓存时,为什么 User.all 会运行?

【问题讨论】:

    标签: ruby-on-rails caching action-caching


    【解决方案1】:

    filter chain halted 消息意味着有一个环绕过滤器可以阻止操作调用。这很可能是动作缓存,它阻止了实际动作的发生。它没有让步该操作,因为它在缓存中找到了一些东西,正如它上面的消息所暗示的那样。

    User.all 根本不应该运行,因为它在操作中,但任何之前的过滤器都会运行。如果您的页面在某种形式的身份验证之后,则身份验证检查可能触发了 SQL 调用。因此,您可能需要仔细检查 SQL 日志的真正来源。

    此外,到期的正确语法(至少根据 rails 指南)是:

    expire_action :action => :index
    

    更多信息:Rails Guide

    【讨论】:

    • 看来你是对的,选择来自我的身份验证...应该仔细看看。感谢您提供有关日志的信息,但对我来说,haltdid not yield 实际上是从缓存中提取的并不明显
    猜你喜欢
    • 2011-07-14
    • 2013-11-27
    • 1970-01-01
    • 2015-05-07
    • 2016-09-01
    • 2015-03-26
    • 2011-08-09
    • 1970-01-01
    • 2011-08-25
    相关资源
    最近更新 更多