How to use 2 different cache backends in Django? How to use 2 different cache backends in Django? django django

How to use 2 different cache backends in Django?


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, cacheinmem_cache = get_cache('inmem')default_cache = get_cache('default')# default_cache == cache 


Since Django 1.9, get_cache is deprecated. Do the following to address keys from 'inmem' (addition to answer by Romans):

from django.core.cache import cachescaches['inmem'].get(key)


In addition to Romans's answer above... You can also conditionally import a cache by name, and use the default (or any other cache) if the requested doesn't exist.

from django.core.cache import cache as default_cache, get_cachefrom django.core.cache.backends.base import InvalidCacheBackendErrortry:    cache = get_cache('foo-cache')except InvalidCacheBackendError:    cache = default_cachecache.get('foo')