Created
May 21, 2013 01:00
-
-
Save unthingable/5616835 to your computer and use it in GitHub Desktop.
local cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Stamped = namedtuple('Stamped', 'stamp obj') | |
cache = {} | |
def cached(ttl, func, *args, **kw): | |
'Simple inline cache for function calls' | |
key = (func.__name__,) + args | |
entry = cache.get(key, None) | |
now = time.time() | |
if not entry or (now - entry.stamp) > ttl: | |
result = func(*args, **kw) | |
entry = cache.setdefault(key, Stamped(now, result)) | |
return entry.obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment