summaryrefslogtreecommitdiff
path: root/main/utils.py
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2010-10-13 17:55:28 -0500
committerDan McGee <dan@archlinux.org>2010-10-13 18:10:55 -0500
commit0c65360e6ccf812ae319b6a70e25804a224cca99 (patch)
tree9d669a1db90b9343f80ca54252948b46bc9e8eee /main/utils.py
parente2612ab3f6963df37f9ca2542718c5712966ca29 (diff)
Add ability to clear a cached function result
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'main/utils.py')
-rw-r--r--main/utils.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/main/utils.py b/main/utils.py
index eadd8550..ae446ab3 100644
--- a/main/utils.py
+++ b/main/utils.py
@@ -5,6 +5,12 @@ except ImportError:
from django.core.cache import cache
from django.utils.hashcompat import md5_constructor
+def cache_function_key(func, args, kwargs):
+ raw = [func.__name__, func.__module__, args, kwargs]
+ pickled = pickle.dumps(raw, protocol=pickle.HIGHEST_PROTOCOL)
+ key = md5_constructor(pickled).hexdigest()
+ return 'cache_function.' + func.__name__ + '.' + key
+
def cache_function(length):
"""
A variant of the snippet posted by Jeff Wheeler at
@@ -19,10 +25,7 @@ def cache_function(length):
"""
def decorator(func):
def inner_func(*args, **kwargs):
- raw = [func.__name__, func.__module__, args, kwargs]
- pickled = pickle.dumps(raw, protocol=pickle.HIGHEST_PROTOCOL)
- key = md5_constructor(pickled).hexdigest()
- key = 'cache_function.' + func.__name__ + '.' + key
+ key = cache_function_key(func, args, kwargs)
value = cache.get(key)
if value is not None:
return value
@@ -33,5 +36,11 @@ def cache_function(length):
return inner_func
return decorator
+def clear_cache_function(func, args, kwargs):
+ key = cache_function_key(func, args, kwargs)
+ cache.delete(key)
+
#utility to make a pair of django choices
make_choice = lambda l: [(str(m), str(m)) for m in l]
+
+# vim: set ts=4 sw=4 et: