flask-caching缓存

  • 为了减少web请求响应时间,并且尽量减少缓存穿透问题,flask-caching插件可以在自己设定时间范围内直接返回结果,而不用去从数据库中查询。
  • 实例化Cache对象
# 实例化Cache对象
from flask_caching import Cache
cache = Cache()
  • flask注册
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):
	...

相关文章:

  • 2021-07-21
  • 2019-11-11
  • 2021-07-09
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
猜你喜欢
  • 2021-11-07
  • 2022-12-23
  • 2018-03-06
  • 2021-05-05
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
相关资源
相似解决方案