gpt4 book ai didi

ruby-on-rails - Rails 低级缓存不缓存

转载 作者:太空宇宙 更新时间:2023-11-03 16:58:57 25 4
gpt4 key购买 nike

我有一个名为事件的模型。在事件模型中,我有以下代码:

def self.all_events
Rails.cache.fetch("events", expires_in: 2.days) do
Event.all.to_a
end
end

我在 Controller 中调用 all_events 方法。如果上面的方法有效,那么服务器日志应该只在第一次调用 Controller 代码时显示查询,之后每次在接下来的两天里,事件应该作为一个数组存在于内存中 - 对吗?出于某种原因,服务器日志每次都显示数据库查询。如何使缓存工作?

最佳答案

您必须在两种环境中正确配置缓存设置。

我更喜欢将 Redis 作为 Heroku 上的生产环境。您可以在开发环境中使用 memory_store 或 file_store 选项。 https://elements.heroku.com/addons/heroku-redis

gem 文件

gem 'redis-rails'

配置/环境/production.rb

Rails.application.configure do
config.action_controller.perform_caching = true
config.cache_store = :redis_store
end

配置/环境/开发.rb

Rails.application.configure do
config.action_controller.perform_caching = true
config.cache_store = :memory_store
end

您可以在那里找到有关在 Ruby on Rails 上进行缓存的更多详细信息; http://guides.rubyonrails.org/caching_with_rails.html

关于ruby-on-rails - Rails 低级缓存不缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46897857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com