我在我的rails 4项目中使用了片段缓存。我有cities controller和city_sweeper
cities_controller.rb
cache_sweeper :city_sweeper, :only => [:update, :destroy]
.
.
def update_featured
@city = City.unscoped.find(params[:id])
if params[:featured]
@city.update_attribute(:featured, params[:featured])
end
render :text => "success:
end
.
end在我的city_sweeper.rb中我有这样的代码
class CitySweeper < ActionController::Caching::Sweeper
observe City
def after_update(city)
expire_cache(city)
end
def after_destroy(city)
expire_cache(city)
end
def after_update_featured(city)
expire_cache(city)
end
def expire_cache(city)
expire_fragment "city_index_#{city.id}"
end
end它可以很好地处理CRUD操作,但是它不能用于调用我的sweeper.rb的自定义method.its,但是我不能在那里获得城市对象。我得到了这个错误:
NoMethodError (undefined method `expire_fragment' for #<CitySweeper:0xab9f1e0 @controller=nil>):发布于 2015-11-03 15:19:09
您可以使用以下命令使fragment cache过期
更新
if @cities.present?
@cities.each do |city|
cache(action: 'recent_update',key: "city_index_#{city.id}", skip_digest: true) do
...
end
end
end在清扫程序中
class CitySweeper < ActionController::Caching::Sweeper
observe City
.....
def expire_cache(city)
expire_fragment(controller: 'cities', action: 'recent_update',key: "city_index_#{city.id}")
end
endhttps://stackoverflow.com/questions/33493032
复制相似问题