Skip to content

Instantly share code, notes, and snippets.

@giter
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giter/6fbb24e402a633b8029a to your computer and use it in GitHub Desktop.
Save giter/6fbb24e402a633b8029a to your computer and use it in GitHub Desktop.
import time
__CACHE = {}
def cache(ns, key, val_func=None , ttl=None):
if ttl is None or ttl <= 0 :
return val_func()
now = int(time.time())
key = ns + ":" + key
val, exp = __CACHE.get(key, (None,-1) )
if (exp < 0 or exp < now) and val_func:
val = val_func()
__CACHE[key] = val, ttl+now
return val
if __name__ == "__main__":
a = 3
v = cache("A", "K", val_func=lambda : a+3 , 300)
print v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment