zoukankan      html  css  js  c++  java
  • flask-caching缓存

    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):
    	...
    
  • 相关阅读:
    20191117-STD::讲解及求平均数例题
    计算机网络-ip分类
    游标cursor
    ajax
    django ORM
    urls
    templates
    views
    models
    setting
  • 原文地址:https://www.cnblogs.com/xujunkai/p/15109782.html
Copyright © 2011-2022 走看看