djangodjango-cachedjango-caching

How to use 2 different cache backends in Django?


I need to use memcached and file based cache. I setup my cache in settings:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': 'c:/foo/bar',
    },
    'inmem': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
}

dummy is temporary. Docs says:

cache.set('my_key', 'hello, world!', 30)
cache.get('my_key')

OK, but how can I now set and get cache only for 'inmem' cache backend (in future memcached)? Documentation doesn't mention how to do that.


Solution

  • CACHES = {
      'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': 'c:/foo/bar',
      },
      'inmem': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
      }
    } 
    
    from django.core.cache import get_cache, cache
    inmem_cache = get_cache('inmem')
    default_cache = get_cache('default')
    # default_cache == cache