equal
deleted
inserted
replaced
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 |