【发布时间】: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