gpt4 book ai didi

ruby-on-rails - 在初始化程序中设置 cache_store

转载 作者:IT王子 更新时间:2023-10-29 05:54:44 25 4
gpt4 key购买 nike

我正在尝试使用 redis-store作为我的 Rails 3 cache_store。我还有一个 initializer/app_config.rb,它加载一个用于配置设置的 yaml 文件。在我的 initializer/redis.rb 我有:

MyApp::Application.config.cache_store = :redis_store, APP_CONFIG['redis'] 

但是,这似乎不起作用。如果我这样做:

Rails.cache

在我的 Rails 控制台中,我可以清楚地看到它正在使用

ActiveSupport.Cache.FileStore

作为缓存存储而不是 redis-store。但是,如果我像这样在我的 application.rb 文件中添加配置:

config.cache_store = :redis_store 

它工作得很好,除了应用程序配置初始化程序是在 application.rb 之后加载的,所以我无权访问 APP_CONFIG。

有人遇到过吗?我似乎无法在初始化程序中设置缓存存储。

最佳答案

some 之后research ,一个可能的解释是 initialize_cache 初始化程序在 rails/initializers 之前运行。所以如果它没有在执行链的前面定义,那么缓存存储就不会被设置。您必须在链的早期配置它,例如在 application.rb 或 environments/production.rb

我的解决方案是在应用配置之前移动 APP_CONFIG 加载:

APP_CONFIG = YAML.load_file(File.expand_path('../config.yml', __FILE__))[Rails.env]

然后在同一个文件中:

config.cache_store = :redis_store, APP_CONFIG['redis']

另一种选择是将 cache_store 放在 before_configuration block 中,如下所示:

config.before_configuration do
APP_CONFIG = YAML.load_file(File.expand_path('../config.yml', __FILE__))[Rails.env]
config.cache_store = :redis_store, APP_CONFIG['redis']
end

关于ruby-on-rails - 在初始化程序中设置 cache_store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5810289/

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