Change Rails cache_store at runtime?

My application has a feature that allows the administrator to change the cache_store configuration using a graphical interface. Then the new configuration will take effect immediately.

By default cache_store in my production.rb:

config.cache_store = :memory_store 

An administrator can optionally change the use of memcached with the Dalli repository. I tried changing Rails.application.config.cache_store:

 Rails.application.config.cache_store = :dalli_store, 'localhost:11211', 'localhost:11212' 

But Rails.cache does not change:

 Rails.cache => <#ActiveSupport::Cache::MemoryStore entries=0, size=0, options={}> 

Is there any way to do this?

+4
source share
1 answer

The cache storage is installed during application initialization and cannot be changed at afaik execution time. To achieve what you are trying to do, you can save the cache storage configuration in the configuration file and restart the entire application. See answers to similar questions to see how to do this.

Disclaimer: with this approach, you can easily kill the application if the configuration is faulty.

+3
source

All Articles