ruby-on-rails - Rails 缓存过期

标签 ruby-on-rails

我有一个 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_inoptions 的一部分争论。
  • implementation of 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/

    相关文章:

    ruby-on-rails - libmysqlclient_r.so.16 : cannot open shared object file: No such file or directory DREAMHOST

    javascript - 类型化js在中间人中不起作用

    ruby-on-rails - 如何以 RESTful 方式更新 has_and_belongs_to_many 集合?

    ruby-on-rails - RSpec:Let 语句有时会出现问题,因为当我执行 get 请求时,我需要它们的值存在于数据库中

    ruby-on-rails - 如何在 activerecord 中实现动态验证

    ruby-on-rails - 如何重定向到并返回 200 状态代码

    ruby-on-rails - 最大值(:created_at) does not give the timestamp in local time format

    ruby-on-rails - Carrierwave 重新创建版本

    ruby-on-rails - Rails 无法从 HTTP 请求获取参数

    ruby-on-rails - Mongoid 关联的自定义键