"Custom cache backend"
# Add following to your settings.py
# ------------
# settings same as for memcached
# CACHE_BACKEND = 'custom_cache://127.0.0.1:11211/'
# CACHE_MIDDLEWARE_KEY_PREFIX = 'site_prefix_'

from django.core.cache.backends import memcached
from django.utils.encoding import smart_unicode, smart_str
from django.conf import settings

class CacheClass(memcached.CacheClass):
    def __init__(self, server, params):
        super(CacheClass, self).__init__(server, params)
        self._prefix = getattr(settings, "CACHE_MIDDLEWARE_KEY_PREFIX", "")
        
    def _key(self, key, is_list=False):
        if is_list:
            keys = []
            for k in key:
                keys.append("%s%s" % (self._prefix, k))
            return keys
        else:
            return "%s%s" % (self._prefix, key)

    def add(self, key, value, timeout=0):
        return super(CacheClass, self).add(self._key(key), value, timeout)

    def get(self, key, default=None):
        return super(CacheClass, self).get(self._key(key), default)

    def set(self, key, value, timeout=0):
        super(CacheClass, self).set(self._key(key), value, timeout)

    def delete(self, key):
        super(CacheClass, self).delete(self._key(key))

    def get_many(self, keys):
        return super(CacheClass, self).get_many(self._key(keys, True))

    def close(self, **kwargs):
        super(CacheClass, self).close(**kwargs)

    def incr(self, key, delta=1):
        return super(CacheClass, self).incr(self._key(key), delta)

    def decr(self, key, delta=1):
        return super(CacheClass, self).decr(self._key(key), delta)