Skip to content

Instantly share code, notes, and snippets.

@samtardif
Created April 12, 2012 06:59
Show Gist options
  • Save samtardif/2365290 to your computer and use it in GitHub Desktop.
Save samtardif/2365290 to your computer and use it in GitHub Desktop.
def cache_method(name):
def dict_cache_wrapper(fn):
cache = {}
def wrapper(*args):
if args not in cache:
cache[args] = fn(*args)
return cache[args]
return wrapper
def memcached_wrapper(fn):
def wrapper(*args):
return fn(*args)
return wrapper
if name == 'memory':
return dict_cache_wrapper
elif name == 'memcached':
return memcached_wrapper
else:
raise Exception
@cache_method("memory")
def lookup(key):
return key
lookup("hello")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment