python flask-caching模块缓存详解

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 flask import Flask, request , render_template_string
from flask_caching import Cache  


app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})

@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  
    )

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-8
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(host='0.0.0.0')  

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

test cache

日志输出:

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

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

日志输出:

123
192.168.211.1 - - [09/Jul/2020 19:01:27] "GET /test HTTP/1.1" 200 -
123
192.168.211.1 - - [09/Jul/2020 19:01:27] "GET /test HTTP/1.1" 200 -
123
192.168.211.1 - - [09/Jul/2020 19:01:27] "GET /test HTTP/1.1" 200 -
123
192.168.211.1 - - [09/Jul/2020 19:01:28] "GET /test HTTP/1.1" 200 -

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ghostwritten

口渴,请赏一杯下午茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值