src/cm/utils/cache.py
changeset 349 8d1ce1bda109
parent 261 b60ab54b6782
equal deleted inserted replaced
348:df389c3a9277 349:8d1ce1bda109
     1 from django.core.cache import cache
     1 from django.core.cache import cache
     2 from hashlib import sha1
     2 from hashlib import sha1
       
     3 from django.conf import settings
     3 
     4 
     4 # adapted [to django] from http://code.activestate.com/recipes/325205/
     5 # adapted [to django] from http://code.activestate.com/recipes/325205/
     5 def dj_memoize(f):
     6 def dj_memoize(f):
     6     def g(*args, **kwargs):
     7     def g(*args, **kwargs):
     7         key = sha1( str((f.__name__, f, tuple(args), frozenset(kwargs.items())) )).hexdigest()
     8         key = sha1( str((settings.SITE_URL, f.__name__, f, tuple(args), frozenset(kwargs.items())) )).hexdigest()
     8         val = cache.get(key)
     9         val = cache.get(key)
     9         if not val:
    10         if not val:
    10             val = f(*args, **kwargs)
    11             val = f(*args, **kwargs)
    11             cache.set(key,val)
    12             cache.set(key,val)
    12         return val
    13         return val