def memoize(func, ttl=86400):
""" A memory caching decorator """
# Local redis as in-memory cache
cache = StrictRedis(host='localhost', port=6379)
def wrapper(*args, **kwargs):
# Construct a unique cache filename
key = unique_key(args[0], args[1])
# Check if its in redis
cached_data = cache.get(key)
if cached_data != None:
print('=>from cache<=')
return json.loads(cached_data)
# Else calculate and store while putting a TTL
result = func(*args, **kwargs)
cache.set(key, json.dumps(result), ttl)
return result
return wrapper
mocking_data.py 文件源码
python
阅读 23
收藏 0
点赞 0
评论 0
评论列表
文章目录