我有一个 Rails 应用程序,因为我使用简单的 Rails 缓存。我的测试如下:
Rails.cache.write('temp',Date.today,:expires_in => 60.seconds)
我可以读完Rails.cache.read('temp')
and Rails.cache.fetch('temp')
.
问题是它不会过期。 60 秒后它仍然活着。谁能告诉我这里缺少什么。
仅供参考:我在development.rb中声明如下:
config.action_controller.perform_caching = true
config.cache_store = :memory_store
有什么我错过的吗?我想让我的缓存过期。
经过一番搜索,我发现了 60 秒后缓存未清理的一个可能原因。
- 你打电话
Rails.cache.write
这是有记录的here http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-write.
- 它调用
write_entry(namespaced_key(name, options), entry, options)
,您的选择:expires_in
是其中的一部分options
争论。
-
The 实施write_entry http://apidock.com/rails/ActiveSupport/Cache/MemCacheStore/write_entry具有以下条件:
if expires_in > 0 && !options[:raw]
# Set the memcache expire a few minutes in the future to support race condition ttls on read
expires_in += 5.minutes
end
所以 60 秒的基础上又增加了 5 分钟。 2种可能的解决方案:
- 忍受它吧:-)
- 尝试包含该选项
:raw => true
,也许这会跳过该条件,以便您的到期如所怀疑的那样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)