/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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
URL: http://www.willmcgugan.com/blog/tech/2007/10/14/timed-caching-decorator/