Rails 缓存过期 [英] Rails cache expire

查看:54
本文介绍了Rails 缓存过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 应用程序,因为我使用的是简单的 rails 缓存.我的测试如下:

I have a rails application, in that I am using simple rails cache. My testing is as follows:

Rails.cache.write('temp',Date.today,:expires_in => 60.seconds)

我可以通过Rails.cache.read('temp')Rails.cache.fetch('temp') 读取它.

I can read it through Rails.cache.read('temp') and Rails.cache.fetch('temp').

问题是它不会过期.它会在 60 秒后仍然活着.谁能告诉我这里缺少什么.

The problem is it doesn't expire. It will still be alive after 60 seconds. Can any one tell me what is missing here.

仅供参考:我在我的 development.rb 中声明如下:

config.action_controller.perform_caching = true

config.cache_store = :memory_store

有什么我遗漏的吗?我想使我的缓存过期.

Is there anything I missed out? I want to expires my cache.

推荐答案

经过一番搜索,我找到了 60 秒后未清除缓存的一个可能原因.

After some search, I have found one possible reason why the cache is not cleaned after 60 seconds.

  1. 您调用了 Rails.cache.write,其中记录了 这里.
  2. 它调用 write_entry(namespaced_key(name, options), entry, options),其中你的选项 :expires_inoptions 的一部分> 争论.
  3. write_entry 的实现具有以下条件:

  1. You call Rails.cache.write which is documented here.
  2. It calls write_entry(namespaced_key(name, options), entry, options), where your option :expires_in is one part of the options argument.
  3. The implementation of write_entry has the following condition:

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 种可能的解决方案:

So there are 5 minutes added to your 60 seconds. 2 possible solutions:

  • 忍受它吧:-)
  • 尝试包含选项 :raw =>是的,也许这会跳过条件,以便您的到期可以正常工作.
  • Just live with it :-)
  • Try to include the option :raw => true, perhaps this will skip the condition, so that your expiry works as suspected.

这篇关于Rails 缓存过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆