Created
January 24, 2013 13:53
-
-
Save linkerlin/4621843 to your computer and use it in GitHub Desktop.
A cache decorator with a ability of lossing memory .
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
def cached(function): | |
cache = {} | |
def wrapper(*args): | |
if args in cache: | |
ret=cache[args] | |
if random.randint(1,100)>95: memo.pop(args) # random forget something | |
return ret | |
else: | |
result = function(*args) | |
cache[args] = result | |
return result | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment