Skip to content

Instantly share code, notes, and snippets.

@unthingable
Created May 21, 2013 01:00
Show Gist options
  • Save unthingable/5616835 to your computer and use it in GitHub Desktop.
Save unthingable/5616835 to your computer and use it in GitHub Desktop.
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