加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails Cache Sweeper

发布时间:2020-12-17 03:09:38 所属栏目:百科 来源:网络整理
导读:我正在尝试实现一个缓存清理程序,它将过滤特定的控制器操作. class ProductsController ActionController caches_action :index cache_sweeper :product_sweeper def index @products = Product.all end def update_some_state #... do some stuff which doe
我正在尝试实现一个缓存清理程序,它将过滤特定的控制器操作.

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 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

ActiveRecord回调’after_save’将正常工作,但似乎永远不会调用控制器操作’after_update_some_state’上的回调.

解决方法

在尝试获取控制器操作的回调时,看起来我只是错过了控制器名称.我的清扫工应该是:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读