gpt4 book ai didi

ruby-on-rails - Rails 缓存清扫器

转载 作者:行者123 更新时间:2023-12-05 00:06:05 27 4
gpt4 key购买 nike

我正在尝试实现一个缓存清扫器,它将过滤特定的 Controller 操作。

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' 将正常工作,但 Controller 操作 'after_update_some_state' 上的回调似乎从未被调用。

最佳答案

看起来我只是在尝试让 Controller 操作的回调工作时缺少 Controller 名称。我的扫地机应该是:

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

关于ruby-on-rails - Rails 缓存清扫器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3771164/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com