2017-11-10 277 views
0

我试图初始化我缓存在application.rb中,使其得到应用在application.rb中

我application.rb中看起来初始化后填充使用Rails.cache为:

config.cache_store = :memory_store 

config.after_initialize do 
    Rails.cache.write('key','value) 
    puts Rails.cache.read('key') 
end 

Rails.cache.read('key')在这里给出一个空值

但是如果我把相同的代码放在rails的其他ruby类中,它会给出预期的输出。

例my_cache.rb

Rails.cache.write('key','value') 
    puts Rails.cache.read('key') # gives output value 

为了提供更多的背景,我也得到这个怪异的行为我Rails.cache在两个application.rb中和my_cache.rb一个ActiveSupport::Cache::NullStore,在这里我想应该是一个ActiveSupport::Cache::MemoryStore

我使用的铁轨5.1.0

回答

0

缓存默认情况下在开发环境禁用。

因此我无法访问缓存。

运行rake dev:cache创建一个空caching-dev.txt,从而使高速缓存在开发环境

这是development.rb怎么看起来像

if Rails.root.join('tmp/caching-dev.txt').exist? 
    config.action_controller.perform_caching = true 
    config.action_mailer.perform_caching = false 
    config.cache_store = :memory_store 
    config.public_file_server.headers = { 
    'Cache-Control' => 'public, max-age=172800' 
    } 
else 
    config.action_controller.perform_caching = false 
    config.action_mailer.perform_caching = false 
    config.cache_store = :null_store 
end 
相关问题