Let publishers configure episode slugs
[mygpo.git] / mygpo / cache.py
blob85dfc1e1f7b2e030528c2b056e38c682f0a151c7
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
12 Usage
14 @cache_result('myfunc', timeout=60)
15 def my_function(a, b, c):
16 pass
17 """
19 def _wrapper(f):
21 @wraps(f)
22 def _get(*args, **kwargs):
24 key = sha1(str(f.__module__) + str(f.__name__) + unicode(args) + unicode(kwargs)).hexdigest()
26 # the timeout parameter can't be used when getting from a cache
27 get_kwargs = dict(cache_kwargs)
28 get_kwargs.pop('timeout', None)
30 value = cache.get(key, NOT_IN_CACHE, **get_kwargs)
32 if value is NOT_IN_CACHE:
33 value = f(*args, **kwargs)
35 cache.set(key, value, **cache_kwargs)
37 return value
40 return _get
42 return _wrapper