skip to Main Content

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


  1. You can easily do it like this using string interpolation.

    Rails.cache.write("#{Rails.env}-some-key", "some-value")
    

    The best is to always use separate caching instance for staging and production environments.

    Login or Signup to reply.
  2. Put the global config into config/environments/{yourenvironment}.rb as following:

    config.cache_store = :mem_cache_store, {namespace: 'yourenvironment-...' }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search