Skip to content

Instantly share code, notes, and snippets.

@linkerlin
Created January 24, 2013 13:53
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 linkerlin/4621843 to your computer and use it in GitHub Desktop.
Save linkerlin/4621843 to your computer and use it in GitHub Desktop.
A cache decorator with a ability of lossing memory .
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