Ruby on rails Rails缓存清理器

Ruby on rails Rails缓存清理器,ruby-on-rails,caching,Ruby On Rails,Caching,我正在尝试实现一个缓存清理器,它将过滤特定的控制器操作 class ProductsController < ActionController caches_action :index cache_sweeper :product_sweeper def index @products = Product.all end def update_some_state #... do some stuff

我正在尝试实现一个缓存清理器,它将过滤特定的控制器操作

class ProductsController < ActionController  
    caches_action :index  
    cache_sweeper :product_sweeper  

    def index 
        @products = Product.all 
    end 

    def update_some_state
      #... do some stuff which doesn't trigger a product save, but invalidates cache
    end
end 
class ProductsController
清扫者等级:

class ProductSweeper < ActionController::Caching::Sweeper
    observe Product

    #expire fragment after model update
    def after_save
       expire_fragment('all_available_products')   
    end

    #expire different cache after controller method modifying state is called.
    def after_update_some_state
        expire_action(:controller => 'products', :action => 'index') 
    end
end
class ProductSweeper'products',:action=>'index')
结束
结束

“保存后”的ActiveRecord回调将正常工作,但控制器操作“更新某些状态后”的回调似乎从未被调用。

我认为您的清理程序应该如下所示:

class ProductSweeper < ActionController::Caching::Sweeper
  observe Product

  def after_save(product)
     expire_cache(product)
  end

  def after_destroy(product)
    expire_cache(product)
  end

  private

  def expire_cache(product)
    expire_fragment('all_available_products')
    expire_page(:controller => 'products', :action => 'index')
  end 

我希望它能帮助你

在尝试使控制器操作的回调正常工作时,我似乎只是缺少了控制器名称。我的清洁工应该是:

class ProductSweeper < ActionController::Caching::Sweeper
    observe Product

    #expire fragment after model update
    def after_save
       expire_fragment('all_available_products')   
    end

    #expire different cache after controller method modifying state is called.
    def after_products_update_some_state
        expire_action(:controller => 'products', :action => 'index') 
    end

    #can also use before:
    def before_products_update_some_state
        #do something before. 
    end
end
class ProductSweeper'products',:action=>'index')
结束
#也可以在以下情况之前使用:
def在产品更新某些状态之前
#做点什么之前。
结束
结束

缓存清理器文档说:“清理器是缓存世界的终结者,负责在模型对象发生变化时使缓存过期。他们通过半观察者、半过滤器和为两个角色实现回调来实现这一点。”这难道不意味着它会将后索引添加为过滤器回调吗?不,清理程序不定义筛选器回调,它们的工作方式几乎与观察者类似,最大的区别(在我看来)是,您必须指定观察者将工作的操作,并且您还可以访问某些特定于控制器的对象和方法,例如:
终止缓存
会话
params
等等……在查看了我的原始帖子之后,我意识到,试图通过使用index作为我试图包装的示例操作来显示我想要完成的任务可能是合谋的。我更新了我的原始示例,使其更加清晰。我更新了我的答案,我认为您有点困惑,清扫器上的
保存后
回调有效,因为您正在观察
传输
,它有
活动记录
回调,例如,
保存后
保存前
在更新之前
,所有这些最终都将在清扫器中触发。。。更新某些状态后,您试图使用的
不是有效的
ActiveRecord
回调,这就是它从未被调用的原因,您应该做的是;在控制器操作上触发回调事件,例如:
@product.save
@product.update\u atributes
,然后使清洗器上的缓存过期。在尝试执行控制器操作回调时,我似乎只是缺少控制器名称。在| before u So之后,可以使用
after u save
方法,而不是调用
after u create
after u update
class ProductSweeper < ActionController::Caching::Sweeper
    observe Product

    #expire fragment after model update
    def after_save
       expire_fragment('all_available_products')   
    end

    #expire different cache after controller method modifying state is called.
    def after_products_update_some_state
        expire_action(:controller => 'products', :action => 'index') 
    end

    #can also use before:
    def before_products_update_some_state
        #do something before. 
    end
end