def memoize(f):

    def newfn(*args, **kwargs):
        mc = memcache.Client(['127.0.0.1:11211'], debug=0)
m = hashlib.md5()
margs = [x.__repr__() for x in args]
mkwargs = [x.__repr__() for x in list(kwargs.values())]
map(m.update, margs + mkwargs)
m.update(f.__name__)
m.update(f.__class__.__name__)
key = m.hexdigest()
value = mc.get(key)
if value:
    return value
else:
    value = f(*args, **kwargs)
mc.set(key, value, 60)
return value
return f(*args)
return newfn

@memoize
def expensive_function(x):
    time.sleep(5)
return x
if __name__ == '__main__':
    print(expensive_function('abc'))
print(expensive_function('abc'))
Using python dictionary as a temporary in-memory key-value database?