我有一个 rails 应用程序,因为我使用的是简单的 rails 缓存。我的测试如下:
Rails.cache.write('temp',Date.today,:expires_in => 60.seconds)
我可以通过
Rails.cache.read('temp')
阅读和 Rails.cache.fetch('temp')
.问题是它不会过期。 60 秒后它仍然存在。谁能告诉我这里缺少什么。
仅供引用:我在 development.rb 中声明如下:
config.action_controller.perform_caching = true
config.cache_store = :memory_store
有什么我错过了吗?我想使我的缓存过期。
最佳答案
经过一番搜索,我找到了 60 秒后没有清理缓存的一个可能原因。
Rails.cache.write
记录在案的 here . write_entry(namespaced_key(name, options), entry, options)
,您的选择:expires_in
是 options
的一部分争论。 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
,也许这会跳过条件,以便您的到期可以正常工作。 关于ruby-on-rails - Rails 缓存过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8846400/