从下面的代码中可以看到。我正在缓存show
操作。我在表演动作View.create_for(@song)
中也有以下方法。
我想这样做,当调用View.create_for(@song)
时,它会清除相应的缓存。
我该怎么做?我必须手动调用View
模型中的轨道清扫器吗?如果是,如何?
我的控制器:
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
我认为您应该指的是songs_ssweeper,而不是views_sweeper:
cache_sweeper :songs_sweeper, :only => [:show]
我不确定你的要求,但你也可以通过将after_save更改为after_create:来在歌曲搜索中更具体
class SongsSweeper < ActionController::Caching::Sweeper
observe Song
def after_create(song)
expire_fragment(/songs/#{song.id}/.*?/)
end
end