from hashlib import sha1 import os,pickle,time defcache_disk(seconds = 900, cache_folder="/tmp"): defdoCache(f): definner_function(*args, **kwargs): # calculate a cache key based on the decorated method signature key = sha1(str(f.__module__) + str(f.__name__) + str(args) + str(kwargs)).hexdigest() filepath = os.path.join(cache_folder, key)
# verify that the cached object exists and is less than $seconds old if os.path.exists(filepath): modified = os.path.getmtime(filepath) age_seconds = time.time() - modified if age_seconds < seconds: return pickle.load(open(filepath, "rb"))
# call the decorated function... result = f(*args, **kwargs)
# ... and save the cached object for next time pickle.dump(result, open(filepath, "wb"))
return result return inner_function return doCache
@cache_disk(seconds = 900, cache_folder="/tmp") defdo_something_time_consuming(n,a): d = {} time.sleep(10) d['name'] = n d['age'] = int(a) return d