Revision: 43411
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 23, 2011 13:08 by gsteff
Initial Code
from datetime import datetime, timedelta
from copy import deepcopy
from threading import RLock
def timed_cache(seconds=0, minutes=0, hours=0, days=0):
time_delta = timedelta( seconds=seconds,
minutes=minutes,
hours=hours,
days=days )
def decorate(f):
f._lock = RLock()
f._updates = {}
f._results = {}
def do_cache(*args, **kwargs):
lock = f._lock
lock.acquire()
try:
key = (args, tuple(sorted(kwargs.items(), key=lambda i:i[0])))
updates = f._updates
results = f._results
t = datetime.now()
updated = updates.get(key, t)
if key not in results or t-updated > time_delta:
# Calculate
updates[key] = t
result = f(*args, **kwargs)
results[key] = deepcopy(result)
return result
else:
# Cache
return deepcopy(results[key])
finally:
lock.release()
return do_cache
return decorate
Initial URL
http://www.willmcgugan.com/blog/tech/2007/10/14/timed-caching-decorator/
Initial Description
Initial Title
Caching decorator
Initial Tags
Initial Language
Python