Skip to content

Instantly share code, notes, and snippets.

@unthingable unthingable/
Created May 21, 2013

What would you like to do?
local cache
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
You can’t perform that action at this time.