Is there a way to ignore Cache errors in Django?

Posted by Josh Smeaton on Stack Overflow See other posts from Stack Overflow or by Josh Smeaton
Published on 2010-12-23T04:33:31Z Indexed on 2011/01/02 10:53 UTC
Read the original article Hit count: 185

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about django

Related posts about caching