Search code examples
djangocaching

Is there a way to ignore Cache errors in Django?


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?


Solution

  • It doesn't look like there is any good way to do what I want, without writing error handing directly into the methods that the cache backend support. Even if init of the backend fails, some backends will only throw errors on first access to the backend.

    What I've done is modified the backend to wrap all methods with error handling that's conditional on a param passed to the constructor. Not as nice as I'd like.. but it's the least intrusive.

    Nothing needs to change in the calling code, so the interface, if you will, is maintained.