Login

Custom Cache with SITE_ID in key

Author:
luke256
Posted:
May 28, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

Wraps existing cache configured as CUSTOM_CACHE_BACKEND and adds the SITE_ID to cache keys.

 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.