首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缓存清理器不适用于自定义方法Rails 4

缓存清理器不适用于自定义方法Rails 4
EN

Stack Overflow用户
提问于 2015-11-03 14:52:16
回答 1查看 387关注 0票数 1

我在我的rails 4项目中使用了片段缓存。我有cities controller和city_sweeper

代码语言:javascript
复制
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中我有这样的代码

代码语言:javascript
复制
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,但是我不能在那里获得城市对象。我得到了这个错误:

代码语言:javascript
复制
 NoMethodError (undefined method `expire_fragment' for #<CitySweeper:0xab9f1e0 @controller=nil>):
EN

回答 1

Stack Overflow用户

发布于 2015-11-03 15:19:09

您可以使用以下命令使fragment cache过期

更新

代码语言:javascript
复制
if @cities.present?
  @cities.each do |city|
    cache(action: 'recent_update',key: "city_index_#{city.id}", skip_digest: true) do
    ...
    end
  end
end

在清扫程序中

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33493032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档