【问题标题】:Rails.cache.fetch returns nilRails.cache.fetch 返回 nil
【发布时间】:2019-09-29 09:03:42
【问题描述】:

我正在尝试在我的 rails 4 应用程序中实现缓存。我安装了redis,根据article设置配置,但每次我尝试在控制器中缓存一些东西时,第一次刷新我得到数据,第二次我得到零。 当我去 redis cli 并执行 KEYS * 时,我看到那个密钥就在那里。

我认为这是我的 redis 问题所以我禁用了 redis 并在 development.rb 中放了

config.cache_store = :memory_store, { size: 128.megabytes }

但如果我尝试

Rails.cache.fetch("dashboard") do
   User.where(:active=>true).size
end

我的操作得到 nil。

我做错了什么缓存总是返回 nil ?

编辑:这是我的redis development.rb 文件的一部分

config.cache_store = :redis_store, {
  expires_in: 1.hour,
  namespace: 'cache',
  redis: { host: 'localhost', port: 6379, db: 0 },
  }

【问题讨论】:

  • 你的 config/development.rb 文件是什么样子的?
  • 我已经用该代码更新了我的问题
  • 在每个 Rails 生成的应用程序中都有一段代码声明 if Rails.root.join('tmp', 'caching-dev.txt').exist? ...,所以如果你没有那个文件,它会将 perform_caching 设置为 false 并将 cache_store 设置为 null。你有吗?
  • 我没有那个文件,所以我创建了空文件,但它没有帮助,仍然是同样的问题

标签: ruby-on-rails ruby-on-rails-4 redis-rails


【解决方案1】:

这只是缓存没有被存储。因此,您必须检查它的配置。 例如。在您的环境文件中,您应该有以下代码 sn-p :

config.action_controller.perform_caching = true
config.cache_store = :media_store

:media_store - 取决于你要将缓存存储在哪里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2013-02-18
    • 2019-06-20
    • 2017-12-16
    • 2014-09-27
    • 2017-06-11
    • 2014-12-29
    相关资源
    最近更新 更多