I have a django application. I want to use both database cache and redis cache at different places in my application. How do I write the settings in settings.py?
How do I import both redis and database cache in my code and set some data into both types of cache?
I tried this in django settings.py
CACHES = {
'default': {
'BACKEND':'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'data-caches',
},
'Redis': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379',
'OPTIONS': {
'db': '1',
'parser_class': 'redis.connection.PythonParser',
'pool_class': 'redis.BlockingConnectionPool',
}
}
}
but when I import in my code from django.core.cache import cache It always picks the default one. How do I import database cache in one python file and redis cache in a different python file?
You need to give your cache name in the cache module while saving and getting the data from the second cache. like this in your case:
from django.core.cache import cache
redis_cache = caches['Redis']
redis_cache.get('key')
Documentation: https://docs.djangoproject.com/en/4.1/topics/cache/#accessing-the-cache