From 0c65360e6ccf812ae319b6a70e25804a224cca99 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 13 Oct 2010 17:55:28 -0500 Subject: Add ability to clear a cached function result Signed-off-by: Dan McGee --- main/utils.py | 17 +++++++++++++---- 1 file 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 @@ 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 inner_func(*args, **kwargs): 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: -- cgit v1.2.3-54-g00ecf