I've just set our development Django site to use redis for a cache backend and it was all working fine. I brought down redis to see what would happen, and sure enough Django 404's due to cache backend behaviour. Either the Connection was refused, or various other errors.
Is there any way to instruct Django to ignore Cache errors, and continue processing the normal way? It seems weird that caching is a performance optimization, but can bring down an entire site if it fails.
I tried to write a wrapper around the backend like so:
class CacheClass(redis_backend.CacheClass):
""" Wraps the desired Cache, and falls back to global_settings default on init failure """
def __init__(self, server, params):
try:
super(CacheClass, self).__init__(server, params)
except Exception:
from django.core import cache as _
_.cache = _.get_cache('locmem://')
But that won't work, since I'm trying to set the cache type in the call that sets the cache type. It's all a very big mess.
So, is there any easy way to swallow cache errors? Or to set the default cache backend on failure?
structure, setting CACHES = None or CACHES['default']['BACKEND'] = None causes Django to choke, and setting CACHES = {} still seems to enable basic caching.
Local Memory Cache Unless we explicitly specify another caching method in our settings file, Django defaults to local memory caching. As its name implies, this method stores cached data in RAM on the machine where Django is running. Local memory caching is fast, responsive, and thread-safe.
To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.
If cache. get() returns the set value it means that cache is working as it should. Otherwise it will return None . An other option is to start memcached with $ memcached -vv , since it will log all the cache accesses to the terminal.
Look at django-cache-fallback:
https://pypi.python.org/pypi/django-cache-fallback/0.2.1
CACHES = {
# Set default cache to FallbackCache
'default': {
'BACKEND': 'cache_fallback.FallbackCache',
},
# Your production main cache (Redis, for example)
'main_cache': {
'BACKEND': 'redis_lock.django_cache.RedisCache',
'LOCATION': redis_url,
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
},
'TIMEOUT': 500,
},
# Use dummy cache to ignore main cache errors and get data from DB
'fallback_cache': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With