正如您从下面的代码中所看到的。我正在缓存show操作。我在show action View.create_for(@song)中也有以下方法。
我想这样做,当View.create_for(@song)被调用时,它会清除相应的缓存。
我该怎么做呢?我必须手动调用View模型中的rails清除器吗?如果是这样的话,是怎么做的?
我的控制器:
class SongsController < ApplicationController
caches_action :show, :cache_path => (proc do
song_path(params[:id], :user_id => user_signed_in? ? current_user.id : nil)
end)
# I tried with the following line, but this is not what I want. I only want to call the sweeper when `View.create_for(@song)` is called:
# cache_sweeper :views_sweeper, :only => [:show]
def show
@song = Song.find(params[:id])
View.create_for(@song)
end
end我的清道夫:
class SongsSweeper < ActionController::Caching::Sweeper
observe Song
def after_save(song)
expire_fragment(/songs\/#{song.id}\/.*?/)
end
end发布于 2012-01-23 03:28:28
我认为你应该指的是songs_sweeper,而不是views_sweeper:
cache_sweeper :songs_sweeper, :only => [:show]我不确定您的需求,但您也可以通过将after_save更改为after_create来在您的SongsSweeper中更具体:
class SongsSweeper < ActionController::Caching::Sweeper
observe Song
def after_create(song)
expire_fragment(/songs\/#{song.id}\/.*?/)
end
endhttps://stackoverflow.com/questions/8939561
复制相似问题