我在rails 4项目中使用片段缓存。我有城市控制器和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,但我没有得到城市对象。我收到了这个错误:
NoMethodError (undefined method `expire_fragment' for #<CitySweeper:0xab9f1e0 @controller=nil>):
答案 0 :(得分:0)
您可以使用此
使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
end