Last active
August 29, 2015 14:05
-
-
Save giter/6fbb24e402a633b8029a to your computer and use it in GitHub Desktop.
This file contains 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
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