author | Alberto Bertogli
<albertito@blitiri.com.ar> 2008-09-01 19:42:06 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2008-09-06 13:54:21 UTC |
parent | 47dc30946c5740f87887ec2e9be76f7c927af490 |
blitiri.cgi | +19 | -11 |
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 = {