django-cachalot icon indicating copy to clipboard operation
django-cachalot copied to clipboard

SUPPORTED_CACHE_BACKENDS should support extending base classes

Open oppianmatt opened this issue 5 years ago • 1 comments

We use a custom cache backend which just extends the base one but provides some extra functionality.

For example:

class MemcachedCache(PyLibMCCache):
    """
    Extends PyLibMCCache to set options and prevent close
    """

    def __init__(self, server, params, username=None, password=None):
        # ensure options is lower case
        try:
            params['OPTIONS'] = {k.lower(): v for k, v in params['OPTIONS'].iteritems()}
            # turn binary back to caps, but default it to true
            params['BINARY'] = params['OPTIONS'].pop('binary', 1)
        except KeyError:
            pass
        super(MemcachedCache, self).__init__(server, params, username, password)

    def close(self, **kwargs):
        """
        Override close to keep connection open
        """
        pass

the list is fixed like so:

SUPPORTED_CACHE_BACKENDS = {
    'django.core.cache.backends.dummy.DummyCache',
    'django.core.cache.backends.locmem.LocMemCache',
    'django.core.cache.backends.filebased.FileBasedCache',
    'django_redis.cache.RedisCache',
    'django.core.cache.backends.memcached.MemcachedCache',
    'django.core.cache.backends.memcached.PyLibMCCache',
}

A small fix could be to check using isinstance perhaps ? and then it should just auto work for people who have custom but extended supported cache backends.

thanks

oppianmatt avatar Jun 07 '19 20:06 oppianmatt

Next version, I'll include a method of flexibility for this.

Andrew-Chen-Wang avatar May 13 '21 05:05 Andrew-Chen-Wang