当前位置: 首页 > 工具软件 > flask-caching > 使用案例 >

Python:Flask缓存扩展Flask-Caching

庄高谊
2023-12-01

文档: https://flask-caching.readthedocs.io/

安装

$ pip install Flask-Caching

代码示例

# -*- coding: utf-8 -*-

"""
flask template

"""
from flask import Flask, request
from flask_caching import Cache

app = Flask(__name__)

# 设置
cache = Cache(config={'CACHE_TYPE': 'simple'})

cache.init_app(app)


# 视图缓存
@app.route('/')
@cache.cached(timeout=50)
def hello_world():
    print('hello_world')
    return 'Hello, World!'


# 显示调用缓存
@app.route('/pre_key')
def get_pre_key():
    key = request.args.get('key')

    pre_key = cache.get('key')
    cache.set('key', key)
    return {'key': pre_key}


if __name__ == '__main__':
    app.run()

 类似资料: