Rails 3:扫描器没有破坏碎片,认为缓存已被禁用

Jan*_*Jan 1 configuration memcached caching fragment-caching ruby-on-rails-3

我想用扫地机过期碎片.执行sweeper回调,但对expire_fragment的调用什么都不做,因为(我假设)cache_configured?返回零.配置缓存并在我的模板中创建和使用片段(在日志中验证它).我究竟做错了什么?

application.rb中

config.cache_store = :mem_cache_store, "XXX.XXX.XXX.XXX", { # I use a real IP
    :compress => true,
    :namespace => "#{Rails.env}_r3"
  }
config.active_record.observers = [:auction_sweeper, :address_sweeper]
Run Code Online (Sandbox Code Playgroud)

production.rb

config.action_controller.perform_caching = true
Run Code Online (Sandbox Code Playgroud)

auction_sweeper.rb

class AuctionSweeper < ActionController::Caching::Sweeper 
  observe Auction

  def after_create(auction)
    Rails.logger.info "AuctionSweeper.expire_details #{auction.id} #{cache_configured?.inspect}=#{perform_caching.inspect}&&#{cache_store.inspect}"
    expire_fragment("auction/#{auction.reference_sid}")
  end
end
Run Code Online (Sandbox Code Playgroud)

在日志文件中,cache_configured?是nil,perform_caching和cache_store也是如此.

AuctionSweeper.expire_details 12732 nil=nil&&nil
Run Code Online (Sandbox Code Playgroud)

所以我假设,我的片段没有过期,因为expire_fragment的代码读取:

文件actionpack/lib/action_controller/caching/fragments.rb,第87行

87:       def expire_fragment(key, options = nil)
88:         return unless cache_configured?
Run Code Online (Sandbox Code Playgroud)

Jan*_*Jan 5

我在这里找到了一个解决方案(黑客?),建议设置@controller,它对我有用.

class AuctionSweeper < ActionController::Caching::Sweeper 
  observe Auction

  def after_create(auction)
    @controller ||= ActionController::Base.new
    Rails.logger.info "AuctionSweeper.expire_details #{auction.id} #{cache_configured?.inspect}=#{perform_caching.inspect}&&#{cache_store.inspect}"
    expire_fragment("auction/#{auction.reference_sid}")
  end
end
Run Code Online (Sandbox Code Playgroud)

另外给自己一个注意事项:记得在清扫器之前从过滤器返回true或者你得到ActiveRecord :: RecordNotSaved并想知道为什么.

  • 谢谢!@controller的黑客为我工作.奇怪的是官方文档中没有提到这一点. (2认同)