From 70a0afa4c3907cd348f4c5699897dfe992150567 Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Mon, 1 Sep 2008 16:42:06 -0300 Subject: [PATCH] Make the cached() decorator take into account all arguments Also, while at it, move the test for cache_path into the decorator, and fix the cache file name. Signed-off-by: Alberto Bertogli --- blitiri.cgi | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/blitiri.cgi b/blitiri.cgi index df29205..347d27a 100755 --- a/blitiri.cgi +++ b/blitiri.cgi @@ -410,22 +410,30 @@ div.section h1 { """ + # Cache decorator +# It only works if the function is pure (that is, its return value depends +# only on its arguments), and if all the arguments are hash()eable. def cached(f): - def decorate(obj, *args, **kwargs): - if cache_path is None: # cache disabled - s = f(obj, *args, **kwargs) - else: - cache_file = os.path.join(cache_path, - 'blitiri.cache.%s.html' % hash(obj)) - try: - s = open(cache_file).read() - except: - s = f(obj, *args, **kwargs) - open(cache_file, 'w').write(s) + # do not decorate if the cache is disabled + if cache_path is None: + return f + + def decorate(*args, **kwargs): + hashes = '-'.join( str(hash(x)) for x in args + + tuple(kwargs.items()) ) + fname = 'blitiri.%s.%s.cache' % (f.__name__, hashes) + cache_file = os.path.join(cache_path, fname) + try: + s = open(cache_file).read() + except: + s = f(*args, **kwargs) + open(cache_file, 'w').write(s) return s + return decorate + # helper functions def rst_to_html(rst, secure = True): settings = { -- 2.43.0