[英]Using Rails.cache in application.rb
我正在嘗試在application.rb中初始化我的緩存,以便在應用初始化后填充它
我的application.rb看起來像:
config.cache_store = :memory_store
config.after_initialize do
Rails.cache.write('key','value)
puts Rails.cache.read('key')
end
Rails.cache.read('key')
在此處給出一個空值
但是,如果將相同的代碼放在其他紅寶石類中,它會提供預期的輸出。
示例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
NullStore,在這里我認為它應該是ActiveSupport::Cache::MemoryStore
我正在使用Rails 5.1.0
默認情況下,在開發環境中禁用緩存。
因此,我無法訪問緩存。
運行rake dev:cache
創建一個空的caching-dev.txt
,從而在dev環境中啟用緩存
這是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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.