python Redis的Flask-Cache示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Redis的Flask-Cache示例相关的知识,希望对你有一定的参考价值。

from flask import Flask
from flask_caching import Cache
import random

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'redis', 'CACHE_REDIS_URL': 'redis://localhost:6379/0'})

@app.route("/route1")
@cache.cached(timeout=10)
def route1():
    data = str(random.randint(0,100000))  + '\n'
    return data

@app.route("/route2")
@cache.cached(timeout=20)
def route2():
    data = str(random.randint(0,100000))  + '\n'
    return data

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8098)

以上是关于python Redis的Flask-Cache示例的主要内容,如果未能解决你的问题,请参考以下文章

zookeeperApache curator的使用及zk分布式锁实现

python 简单类型的Flask-Cache示例

Flask-Cache 怎么缓存动态内容

用 Flask 来写个轻博客 (27) — 使用 Flask-Cache 实现网页缓存加速

python memoize / cache decorators(另请参阅https://pythonhosted.org/Flask-Cache/#flask.ext.cache.Cache.me

Python的Flask框架使用Redis做数据缓存的配置方法