从下面的代码中可以看到。我正在缓存show
行动。我在show action View.create_for(@song)
中也有以下方法。
我想这样做,当调用View.create_for(@song)
时,它会清除相应的缓存。
我该怎么做?我是否必须在View
模型中手动调用rails sweeper?如果是这样,怎么样?
我的控制器:
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
答案 0 :(得分:1)
我认为你应该指的是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
end