在项目的app下新建目录sweeper,在该目录下新建sweeper类的文件,如listen_sweeper.rb class
class ListenSweeper < ActionController::Caching::Sweeper
observer User, Production #observer Model you wanted
def after_save(record)
end
def after_update(record)
end
def after_destroy(record)
end
private
def do_something
expire_page(:controller => "lists", :action => %w( show public feed ), :id => list.id)
expire_action(:controller => "lists", :action => "all")
end
end
在相应的controller中,
cache_sweeper :listen_sweeper, :only => [:action_one, :action_two]
需要在config/application.rb中加载
config.autoload_paths += %W(#{Rails.root}/app/sweepers)
本文介绍如何在Rails应用中创建一个监听缓存清除器,通过定义特定的观察者和缓存清除规则来确保数据的一致性和高效更新。具体包括在不同模型事件触发时进行页面缓存的清除。
819

被折叠的 条评论
为什么被折叠?



