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.
2
Answers
You can easily do it like this using string interpolation.
The best is to always use separate caching instance for staging and production environments.
Put the global config into config/environments/{yourenvironment}.rb as following: