python flask-caching模块缓存详解

2022-10-05 08:36:48

python 模块 flask-caching 缓存


1. 介绍

为了尽量减少缓存穿透,同时减少web的响应时间,我们可以针对那些需要一定时间才能获取结果的函数和那些不需要频繁更新的视图函数提供缓存服务,可以在一定的时间内直接返回结果而不是每次都需要计算或者从数据库中查找。flask_caching插件就是提供这种功能的神器。

2. 安装

$ easy_install Flask-Caching

$ pip install Flask-Caching

3. 配置参数

CACHE_TYPE:设置缓存的类型# 下面五个参数是所有的类型共有的
CACHE_NO_NULL_WARNING="warning"# null类型时的警告消息
CACHE_ARGS=[]# 在缓存类实例化过程中解包和传递的可选列表,用来配置相关后端的额外的参数
CACHE_OPTIONS={}# 可选字典,在缓存类实例化期间传递,也是用来配置相关后端的额外的键值对参数
CACHE_DEFAULT_TIMEOUT# 默认过期/超时时间,单位为秒
CACHE_THRESHOLD# 缓存的最大条目数

CACHE_TYPE= null# 默认的缓存类型,无缓存
CACHE_TYPE='simple'# 使用本地python字典进行存储,线程非安全

CACHE_TYPE='filesystem'# 使用文件系统来存储缓存的值
CACHE_DIR=""# 文件目录

CACHE_TYPE='memcached'# 使用memcached服务器缓存
CACHE_KEY_PREFIX# 设置cache_key的前缀
CAHCE_MEMCACHED_SERVERS# 服务器地址的列表或元组
CACHE_MEMCACHED_USERNAME# 用户名
CACHE_MEMCACHED_PASSWORD# 密码

CACHE_TYPE='uwsgi'# 使用uwsgi服务器作为缓存
CACHE_UWSGI_NAME# 要连接的uwsgi缓存实例的名称

CACHE_TYPE='redis'# 使用redis作为缓存
CACHE_KEY_PREFIX# 设置cache_key的前缀
CACHE_REDIS_HOST# redis地址
CACHE_REDIS_PORT# redis端口
CACHE_REDIS_PASSWORD# redis密码
CACHE_REDIS_DB# 使用哪个数据库# 也可以一键配置
CACHE_REDIS_URL    连接到Redis服务器的URL。示例redis://user:password@localhost:6379/2

4. cache方法

cache.cached:装饰器,装饰无参数函数,使得该函数结果可以缓存
参数:
timeout:超时时间
key_prefix:设置该函数的标志
unless:设置是否启用缓存,如果为True,不启用缓存
forced_update:设置缓存是否实时更新,如果为True,无论是否过期都将更新缓存
query_string:为True时,缓存键是先将参数排序然后哈希的结果

cache.memoize:装饰器,装饰有参数函数,使得该函数结果可以缓存
make_name:设置函数的标志,如果没有就使用装饰的函数# 其他参数同cached

cache.delete_memoized:删除缓存
参数:
fname:缓存函数的名字或引用*args:函数参数

cache.clear()# 清除缓存所有的缓存,这个操作需要慎重
cache.cache# 获取缓存对象#获取某个网页是否存在缓存,key值如'view//gbook.html'
cache.cache.has('view/{}'.format(request.path))#打印该缓存print(request.path,cache.get('view/{}'.format(request.path)))#删除该缓存
cache.delete('view//gbook.html')

5. 显示缓存存储

利用cache.getcache.set方法

from flaskimport Flask, request, render_template_stringfrom flask_cachingimport Cache  


app= Flask(__name__)
cache= Cache(app, config={'CACHE_TYPE':'simple'})@app.route("/html")@app.route("/html/<foo>")defhtml(foo=None):if fooisnotNone:  
       cache.set("foo", foo)  
       bar= cache.get("foo")return render_template_string("<html><body>foo cache: {{bar}}</body></html>", bar=bar)if __name__=='__main__':
   app.run(host='0.0.0.0')

执行python cache1.py, 访问:http://127.0.0.1:5000/html/hello

foo cache: hello

6. 缓存20秒效果

使用装饰器 cached() 能够缓存等待

#---coding:utf-8from flaskimport Flask, requestfrom flask_cachingimport Cache  
  
app= Flask(__name__)# simple使用字典存储
cache= Cache(app, config={'CACHE_TYPE':'simple'})@app.route('/index')@cache.cached(timeout=20)defindex():print(request.path)  
    s='test cache'  
    cache.set('b',123)print('test cache')return s@app.route('/test')deftest():print(cache.get('b'))return'ok'if __name__=='__main__':  
   app.run(host='0.0.0.0')

执行python cache2.py ,访问:http://127.0.0.1:5000/index
界面输出,多次刷新

test cache

日志输出:

test cache192.168.211.1--[09/Jul/202018:59:39]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:42]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:43]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:43]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:46]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:47]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:49]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:49]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:51]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:52]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:53]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:54]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:55]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:56]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:56]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:57]"GET /index HTTP/1.1"200-192.168.211.1--[09/Jul/202018:59:58]"GET /index HTTP/1.1"200-/index
test cache192.168.211.1--[09/Jul/202018:59:59]"GET /index HTTP/1.1"200-

访问http://127.0.0.1:5000/test,多次刷新
界面输出:ok

日志输出:

123192.168.211.1--[09/Jul/202019:01:27]"GET /test HTTP/1.1"200-123192.168.211.1--[09/Jul/202019:01:27]"GET /test HTTP/1.1"200-123192.168.211.1--[09/Jul/202019:01:27]"GET /test HTTP/1.1"200-123192.168.211.1--[09/Jul/202019:01:28]"GET /test HTTP/1.1"200-

参考:

  • 作者:ghostwritten
  • 原文链接:https://ghostwritten.blog.csdn.net/article/details/107235464
    更新时间:2022-10-05 08:36:48