mocking_data.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:Software-Architecture-with-Python 作者: PacktPublishing 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号