Login

Custom Cache with SITE_ID in key

Author:
luke256
Posted:
May 28, 2009
Language:
Python
Version:
1.0
Tags:
cache site_id cache-backend
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. Function cache decorator by bradbeattie 2 years, 3 months ago
  2. Dynamic SITE_ID thread-safe by jhg 10 months, 2 weeks ago
  3. Cached template filters by coleifer 4 years, 9 months ago
  4. Caching Decorator by spenczar 2 years, 10 months ago
  5. change SITE_ID on fly by m-garanin@yandex.ru 6 years, 5 months ago

Comments

Please login first before commenting.