除非手动调用,否则扫地机无法正常工作,发生了什么?

时间:2012-05-05 21:02:02

标签: ruby-on-rails ruby-on-rails-3

我有以下设置:名为Categories的模型。使用ActiveAdmin管理这些类别。我想使用页面缓存来缓存类别。

这就是我设置app / admin / categories.rb

的方法
ActiveAdmin.register Category do
    controller do 
        cache_sweeper :category_sweeper
    end
end

这是我的清扫工:

class CategorySweeper < ActionController::Caching::Sweeper
  observe Category

  def after_save(category)
    expire_cache(category)
  end

  def after_destroy(category)
    expire_cache(category)
  end

  def expire_cache(category)
    expire_page :controller => 'categories', :action => 'index', :format => 'json'
  end
end

这是我的控制器:

class CategoriesController < ApplicationController
    caches_page :index
  cache_sweeper :category_sweeper

    respond_to :json

    def index
      @categories = Category.all 
      respond_with(@categories)
    end

    def show
      CategorySweeper.instance.expire_cache(@category)
      respond_with('manually sweeped!')
    end
end

所以这个想法是当活动管理员发生变化时应该调用清扫工具。我设置了debug.log,结果证明它正常工作。但由于某种原因,缓存不会过期!

但是,如果我执行show动作(即转到/categories/1.json,那么我的手动清扫器就会启动并且工作正常)。那么为什么清扫器只在我调用它时工作,而不是在管理员发生变化时呢?

提前致谢, -David

1 个答案:

答案 0 :(得分:2)

您的ActiveAdmin控制器位于不同的命名空间中,请参阅this SO问题。

不久:

将斜杠添加到缓存过期网址代码中:

def expire_cache(category)
  expire_page :controller => '/categories', :action => 'index', :format => 'json'
end