1 安装三方库 pip install flask_cache
2 代码实现
# --*-- encoding:utf8 --*--
from flask import Flask, jsonify
from flask_cache import Cache
cache = Cache()
class config():
# 缓存的存储类型
CACHE_TYPE = 'redis'
# 缓存的ip, 端口, 数据库
CACHE_REDIS_HOST = '127.0.0.1'
CACHE_REDIS_PORT = 6379
CACHE_REDIS_DB = 5
app = Flask(__name__)
app.config.from_object(config)
cache.init_app(app)
@app.route('/test/', methods=["get"])
@cache.cached(timeout=5)
def test():
msg = "hello cache"
print(msg)
return jsonify(msg=msg)
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=9003)