flask-caching缓存
- 为了减少web请求响应时间,并且尽量减少缓存穿透问题,flask-caching插件可以在自己设定时间范围内直接返回结果,而不用去从数据库中查询。
- 实例化Cache对象
# 实例化Cache对象
from flask_caching import Cache
cache = Cache()
def init_cache(app):
from .cache.cache import cache
cache.config = {
"CACHE_TYPE": app.config["CACHE_TYPE"],
"CACHE_REDIS_HOST": app.config["CACHE_REDIS_HOST"],
"CACHE_REDIS_PORT": app.config["CACHE_REDIS_PORT"],
"CACHE_REDIS_PASSWORD": app.config["CACHE_REDIS_PASSWORD"],
"CACHE_REDIS_DB": app.config["CACHE_REDIS_DB"]
}
cache.init_app(app)
init_cache(app)
@api.route("/TopN/<timer>/", methods=["GET"], endpoint="sleep_bed_statistics_top_n")
@cache.memoize(timeout=60, make_name='demo1')
def demo1(timer):
...