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]
Production.rbuction_sweeper.rb
config.action_controller.perform_caching = true
?
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
在日志文件中,cache_configured 为 nil,perform_caching 和 cache_store 也为 nil。
AuctionSweeper.expire_details 12732 nil=nil&&nil
所以我假设我的片段没有过期,因为expire_fragment的代码如下:
文件actionpack/lib/action_controller/caching/fragments.rb,第87行
87: def expire_fragment(key, options = nil)
88: return unless cache_configured?
I want to expire fragments with a sweeper. The sweeper callbacks are executed, but the calls to expire_fragment do nothing, because (I assume) cache_configured? returns nil. Caching is configured and fragments are being created and used in my templates (verified it in the logs). What am I doing wrong?
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]
production.rb
config.action_controller.perform_caching = true
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
In log files, cache_configured? is nil and so is perform_caching and cache_store.
AuctionSweeper.expire_details 12732 nil=nil&&nil
So I assume, that my fragments are not expired because the code of expire_fragment reads:
File actionpack/lib/action_controller/caching/fragments.rb, line 87
87: def expire_fragment(key, options = nil)
88: return unless cache_configured?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个解决方案(黑客?) 这里建议设置@controller,它对我有用。
还有一个给我自己的注释:记得在清理器中的过滤器之前返回 true,否则你会得到 ActiveRecord::RecordNotSaved 并想知道为什么。
I found a solution (hack?) here that suggests to set @controller and it works for me.
Also a note to myself: remember to return true from before filters also in sweepers or you get ActiveRecord::RecordNotSaved and wonder why.
您没有共享您的
Rails.env
- 缓存在development
和test
中自然被禁用 - 也许您错过了一些东西?You didn't share your
Rails.env
- caching is naturally disabled indevelopment
andtest
- maybe you missed something?我没有为控制器设置实例变量,而是使用一个名为cache_sweeping_observer.rb的初始化程序
,然后我为任何用于清除缓存的观察者继承它。
Instead of setting the instance variable for the controller I use an initializer called cache_sweeping_observer.rb
Then I inherit from it for any observer that is for sweeping caches.