Flask - 缓存插件 - flask-cache

目录

一、参考文档

1-1 cache的使用错误信息 - flask.ext不存在

二、flask-caching的显示缓存存储

三、简单使用


一、参考文档

官方文档

flask_caching github

flask_cacheing官方文档

1-1 cache的使用错误信息 - flask.ext不存在

解决方式

from flask_cache import Cache
替换成
from flask_caching import Cache

二、flask-caching的显示缓存存储

可以使用代理方法(如 Cache.set() 和 Cache.get() 直接)显式缓存数据 。

@app.route("/html")
@app.route("/html/<foo>")
def html(foo=None):
    if foo is not None:
        cache.set("foo", foo)
    bar = cache.get("foo")
    return render_template_string(
        "<html><body>foo cache: {{bar}}</body></html>", bar=bar
    )

三、简单使用

from flask import Flask, request
from flask_caching import Cache

app = Flask(__name__)
# simple使用字典存储
cache = Cache(app, config={'CACHE_TYPE': 'simple'})


@app.route('/index')
@cache.cached(timeout=20)
def index():
    print(request.path)
    s = 'test cache'
    cache.set('b',123)
    print('test cache')

    return s


@app.route('/test')
def test():
    print(cache.get('b'))
    return 'ok'


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

猜你喜欢

转载自blog.csdn.net/qq_33961117/article/details/88989414