ruby-on-rails – Rails caches_action正在跳过before_filters

我有一个控制器,我正在缓存展示动作.显示操作有一些以前的安全性过滤器,用于执行并重定向,如果用户未登录,而不是当前组的成员等.这些过滤器在我没有开启缓存时工作正常,但是当我翻转开关以在之前的过滤器不再执行(我的调试器调用没有命中)的缓存.

一直以来我的理解是,在过滤器被称为缓存操作之前,这是页面缓存和动作缓存之间的主要区别.这是由Rails Caching Tutorial section on action caching支持,内容如下:

Action Caching works like Page Caching except for the fact that the incoming web request does go from the webserver to the Rails stack and Action Pack so that before filters can be run on it before the cache is served. This allows authentication and other restriction to be run while still serving the result of the output from a cached copy.

那么为什么我以前的过滤器被调用?

有关我的设置:Rails 3.1使用Devise进行身份验证.我正在使用dalli宝石作为一个memcached商店.

这是一些代码,总结了我的代码(很多cruft切出):

class GroupsController < ApplicationController
  caches_action :show
  cache_sweeper :group_sweeper

  before_filter :authenticate_user!,:except => [:index]
  before_filter :init_group,:except => [:new,:create,:index]
  before_filter :requires_group_membership,:index]

  def show
  end

  private

  def requires_group_membership
    if current_user and !@group.users_active.index(current_user).nil?
      return true
    else
      redirect_to :root
      return false
    end
  end

  def init_group
    @group = current_user.active_groups.find_by_id(params[:id])

    if @group.nil?
      redirect_to :root
      return false 
    end
  end

那么以前有谁看过这个行为吗?我有一个洞,我理解过滤器和动作缓存应该如何工作?或者也许我有一些奇怪的voodo发生与奇怪的宝石版本组合?

[编辑]

有趣的是,我刚刚知道,返回值对于是否运行链条进一步的方法没有影响,是否调用重定向或渲染.

[编辑2]

我升级了我的应用程序到轨道3.2.3看看它是否有影响,但没有解决问题.我发现的一些事情是ApplicationController中定义的以前的过滤器被调用,但是我的GroupsController中没有.

解决方法

这是一个非常时间的方式来学习关于缓存的新tidbit.

事实证明,您必须在要运行的before_filters之前调用caches_action.我将缓存操作作为我课程中的第一件事情之一,所以以前的所有过滤器都没有像下面的那样运行.

感谢Pan Thomakoshis answer包含这个宝石的信息 – 这不是在红宝石文档或我已经撇去了它.一旦我设法克服了由于这个小盲点造成的时间损失,我将尽力将这个信息添加到文档中.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...