Custom Cache with SITE_ID in key

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# This acts as a wrapper around the django cache. 
# It adds the current SITE_ID to the keys
# 
# Add following to your settings.py
# ------------
# CACHE_BACKEND = 'custom_cache://'
# CUSTOM_CACHE_BACKEND = 'locmem:///?timeout=300&max_entries=6000'

from django.conf import settings
from django.core.cache import get_cache
from django.core.cache.backends.base import BaseCache

WRAPPED_CACHE = get_cache(settings.CUSTOM_CACHE_BACKEND)

class CacheClass(BaseCache):
    
    def __init__(self, *args):
        # nothing
        pass

    def add(self, key, *args):
        return WRAPPED_CACHE.add(self._key(key),*args)

    def get(self, key, *args):
        return WRAPPED_CACHE.get(self._key(key),*args)

    def set(self, key, *args):
        return WRAPPED_CACHE.set(self._key(key),*args)

    def delete(self, key):
        return WRAPPED_CACHE.delete(self._key(key))

    def get_many(self, keys):
        keys = [self._key(key) for key in keys]
        return WRAPPED_CACHE.get_many(keys)
        
    def has_key(self, key):
        return WRAPPED_CACHE.has_key(self._key(key))

    def __contains__(self, key):
        return WRAPPED_CACHE.__contains__(self._key(key))
        
    def _key(self,key):
        return "%s|%s" % (settings.SITE_ID, key)

More like this

  1. change SITE_ID on fly by m-garanin@yandex.ru 4 years, 7 months ago
  2. Multiple cache backend by isaidyep 2 years, 8 months ago
  3. Binding signals to abstract models by andreterra 1 year ago
  4. Another Memcache Status View by cmheisel 5 years, 3 months ago
  5. Check Size of Object in memcached by deryck 5 years, 6 months ago

Comments

(Forgotten your password?)