1 from hashlib
import sha1
2 from django
.core
.cache
import cache
3 from functools
import wraps
6 NOT_IN_CACHE
= object()
9 def cache_result(**cache_kwargs
):
10 """ Decorator to cache the result of a function call
14 @cache_result('myfunc', timeout=60)
15 def my_function(a, b, c):
22 def _get(*args
, **kwargs
):
24 key
= sha1(str(f
.__module
__) + str(f
.__name
__) +
25 unicode(args
) + unicode(kwargs
)).hexdigest()
27 # the timeout parameter can't be used when getting from a cache
28 get_kwargs
= dict(cache_kwargs
)
29 get_kwargs
.pop('timeout', None)
31 value
= cache
.get(key
, NOT_IN_CACHE
, **get_kwargs
)
33 if value
is NOT_IN_CACHE
:
34 value
= f(*args
, **kwargs
)
36 cache
.set(key
, value
, **cache_kwargs
)