我的站点索引上有动作缓存,并设置了一个运行良好的 SiteSweeper:
# app/controllers/admin/sites_controller.rb
class Admin::SitesController < Admin::BaseController
cache_sweeper :site_sweeper, :only => [:create, :update, :destroy]
caches_action :index, :cache_path => '/admin/sites'
...
# app/sweepers/site_sweeper.rb
class SiteSweeper < ActionController::Caching::Sweeper
observe Site
def after_save(site)
expire_cache(site)
end
def after_destroy(site)
expire_cache(site)
end
def expire_cache(site)
expire_action '/admin/sites'
end
end
但是我也想在保存或销毁任何发布者时使 /admin/sites 过期。是否可以让 PublisherSweeper 用类似的方式使站点索引过期?
# app/sweepers/publisher_sweeper.rb
class PublisherSweeper < ActionController::Caching::Sweeper
observe Publisher
def after_save(publisher)
expire_cache(publisher)
end
def after_destroy(publisher)
expire_cache(publisher)
end
def expire_cache(publisher)
expire_action '/admin/sites'
end
end
我知道我可以在各种发布者操作中调用 expire_action '/admin/sites' 。我只是想知道扫地机是否有这种能力(让我的控制器更干净一些)。