在初始化程序中设置cache_store

我正在尝试使用redis-store作为我的Rails 3 cache_store。 我还有一个初始化器/ app_config.rb,它为配置设置加载一个yaml文件。 在我的初始化程序/ 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 

它运行得很好,除了在app.rb之后加载app config初始化程序,所以我没有访问APP_CONFIG。

有没有人经历过这个? 我似乎无法在初始化程序中设置缓存存储。

经过一些 研究 ,可能的解释是initialize_cache初始化器在rails / initializers之前运行。 因此,如果之前未在执行链中定义,则不会设置缓存存储。 您必须在链中更早地配置它,例如在application.rb或environments / production.rb中

我的解决方案是在app配置之前移动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块中,如下所示:

 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 

我尝试了以下内容,但它确实有效。

 MyApp::Application.config.cache_store = :redis_store self.class.send :remove_const, :RAILS_CACHE if self.class.const_defined? :RAILS_CACHE RAILS_CACHE = ActiveSupport::Cache.lookup_store(MyApp::Application.config.cache_store) 

在原始设置中,如果您更改它会有所帮助:

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

至:

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

config/initializers Rails.cache后运行config/initializers ,但是在config/application.rbconfig/environments

config / application.rb或环境中的配置

因此,一种解决方案是在config/application.rbconfig/environments/*.rbconfig/application.rb缓存。

在config / initializers / cache.rb中配置

如果要在初始化程序中有意配置缓存,可以通过在配置后手动设置Rails.cache来完成:

 # config/initializers/cache.rb Rails.application.config.cache_store = :redis_store, APP_CONFIG['redis'] # Make sure to add this line (http://stackoverflow.com/a/38619281/2066546): Rails.cache = ActiveSupport::Cache.lookup_store(Rails.application.config.cache_store) 

添加规格

为了确保它有效,添加如下规范:

 # spec/features/smoke_spec.rb require 'spec_helper' feature "Smoke test" do scenario "Testing the rails cache" do Rails.cache.write "foo", "bar" expect(Rails.cache.read("foo")).to eq "bar" expect(Rails.cache).to be_kind_of ActiveSupport::Cache::RedisStore end end 

更多的信息

  • 在应用程序引导期间设置Rails.cache : https : //github.com/rails/rails/blob/5-0-stable/railties/lib/rails/application/bootstrap.rb#L62L70 。 redis商店不响应:middleware 。 因此,我们可以省去额外的行。
  • 另见: https : //github.com/rails/rails/issues/10908#issuecomment-19281765
  • http://guides.rubyonrails.org/caching_with_rails.html#cache-stores

有同样的问题,并将RAILS_CACHE设置为MyApp::Application.config.cache_store也是如此。

在初始化程序中

REDIS ||= Rails.configuration.redis_client

在application.rb中

 config.redis_client = Redis.new({ :host => ENV["REDIS_HOST"], :port => ENV["REDIS_PORT"], :db => ENV["REDIS_DB"].to_i, }) config.cache_store = :redis_store, { client: config.redis_client }