ruby-on-railsmemcached

How to configure Rails caching to namespace cache keys with the environment?


I'm deploying a Rails 6.1 App on a server that hosts both my staging and my production environments, and has a unique memcached instance.

If I don't namespace all cache keys, I believe the same keys will be generated in production and staging. Tests on my staging environment will impact production.

I know I could pass a namespace for each call:

Rails.cache.write("some-key", "some-value", namespace: Rails.env)

But it's error-prone.

I'm looking for a global config saying that all keys should be namespaced with the environment.


Solution

  • Put the global config into config/environments/{yourenvironment}.rb as following:

    config.cache_store = :mem_cache_store, {namespace: 'yourenvironment-...' }