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