flask扩展 flask-cache

2022-09-14 18:57:13 浏览数 (1)

一、安装

pip install flask-cache

二、创建三方对象

代码语言:javascript复制
from flask_cache import Cache
cache = Cache()

三、app加载三方对象

代码语言:javascript复制
from exts import cache
cache.init_app(app)

四、配置

代码语言:javascript复制
<span class="hljs-comment"># 缓存类型 redis数据库</span>
CACHE_TYPE = <span class="hljs-string">"redis"</span>
<span class="hljs-comment"># CACHE_TYPE = "simple"  # 简单的缓存</span>
<span class="hljs-comment">#缓存时间</span>
CACHE_DEFAULT_TIMEOUT = <span class="hljs-number">60</span> * <span class="hljs-number">60</span> * <span class="hljs-number">24</span> * <span class="hljs-number">7</span>
<span class="hljs-comment">#redis中键的前缀</span>
CACHE_KEY_PREFIX = <span class="hljs-string">"cache:"</span>
<span class="hljs-comment"># redis地址</span>
CACHE_REDIS_HOST = <span class="hljs-string">"127.0.0.1"</span>
<span class="hljs-comment"># redis端口</span>
CACHE_REDIS_PORT = <span class="hljs-number">6379</span>
<span class="hljs-comment"># redis密码</span>
CACHE_REDIS_PASSWORD = <span class="hljs-string">"123456"</span>
<span class="hljs-comment"># 使用哪个库</span>
CACHE_REDIS_DB = <span class="hljs-number">1</span>

五、启动报错与解决

  • 报错

解决

打开文件

sudo vim /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_cache/jinja2ext.py

修改文件

33行原文件

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask.ext.cache <span class="hljs-keyword">import</span> make_template_fragment_key

修改为

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask_cache <span class="hljs-keyword">import</span> make_template_fragment_key

六、使用

导入

代码语言:javascript复制
<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> cache

装饰器缓存

cache.cached()

  • timeout:过期时间,默认为None,即永不过期
  • key_prefix:缓存项键值的前缀,默认为”view/%s” 传入的值是唯一的
  • unless:回调函数,当其返回True时,缓存不起作用。默认为None,即缓存有效
代码语言:javascript复制
<span class="hljs-meta">@myApp.route('/index/')</span>
<span class="hljs-meta">@cache.cached(timeout=300)</span>
<span class="hljs-comment"># @cache.cached(timeout=300,key_prefix='index')# 设置一个key_prefix来作为标记</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"index.html"</span>)

注意

  • cache.delete(‘index’) 前缀index来删除缓使用
  • 装饰器 cache.cached() 不会考虑到参数的问题, 如果函数在接收到不同的实参后依然返回相同结果

cache.memoize()

代码语言:javascript复制
<span class="hljs-meta">@myApp.route('/page/&lt;int:num&gt;/')</span>
<span class="hljs-meta">@myApp.route('/page/')</span>
<span class="hljs-meta">@cache.memoize(300)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">con</span><span class="hljs-params">(num=<span class="hljs-number">1</span>)</span>:</span>
    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"index.html"</span>)

注意

  • 装饰器 @cache.memoize() 不仅仅会缓存运行的结果, 还缓存调用时的参数, 所以在函数接受到相同的参数时, 就会将缓存中该参数对应的结果返回

底层缓存

设置

代码语言:javascript复制
<span class="hljs-meta">@myApp.route("/setCache/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setCache</span><span class="hljs-params">()</span>:</span>
    key = request.args.get(<span class="hljs-string">"key"</span>)
    value = request.args.get(<span class="hljs-string">"value"</span>)
    <span class="hljs-comment">#设置缓存(往redis中存储键值对)</span>
    cache.set(key, value, <span class="hljs-number">60</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-string">"设置了一个键为%s,值为%s的缓存"</span>%(key, value)

获取

代码语言:javascript复制
<span class="hljs-meta">@myApp.route("/getCache/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getCache</span><span class="hljs-params">()</span>:</span>
    key = request.args.get(<span class="hljs-string">"key"</span>)
    <span class="hljs-comment">#获取缓存值,如果获取不到返回None</span>
    value = cache.get(key)
    <span class="hljs-keyword">return</span> <span class="hljs-string">"获取键为%s的缓存的值:%s"</span>%(key, value)

删除

第一种方法就是设置过期时间自动清除,可以在 Flask 的config里面加上配置项:

CACHE_DEFAULT_TIMEOUT

装饰器加参数timeout=50。

代码语言:javascript复制
<span class="hljs-meta">@cache.cached(timeout=50)</span>
<span class="hljs-meta">@cache.memoize(timeout=50)</span>
清除所有缓存
代码语言:javascript复制
cache.clear() 

只清除 memoize的缓存

代码语言:javascript复制
cache.delete_memoized(缓存的视图函数名)
<span class="hljs-comment">#实例</span>
cache.delete_memoized(con)
cache.delete_memoized(<span class="hljs-string">'con'</span>, <span class="hljs-number">5</span>)    
<span class="hljs-comment"># 删除调用'con'函数并且参数为5的缓存项</span>

删除cache.cached的缓存

代码语言:javascript复制
cache.delete(<span class="hljs-string">'con'</span>) <span class="hljs-comment"># 删除'con'缓存项</span>
cache.delete_many(<span class="hljs-string">'con'</span>, <span class="hljs-string">'view_hello'</span>)  <span class="hljs-comment"># 同时删除'con'和'view_hello'缓存项</span>

缓存案例

代码语言:javascript复制
<span class="hljs-meta">@blue.route("/user/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user</span><span class="hljs-params">()</span>:</span>
    addr = request.remote_addr
    key = addr   <span class="hljs-string">"user"</span>
    <span class="hljs-comment"># current_app</span>
    result = cache.get(key)
    <span class="hljs-keyword">if</span> result:
        print(addr, <span class="hljs-string">"从缓存中加载数据"</span>)
        <span class="hljs-keyword">return</span> result
    result = render_template(<span class="hljs-string">"User.html"</span>)
    print(addr, <span class="hljs-string">"从数据库加载数据"</span>)
    cache.set(key, result, timeout=<span class="hljs-number">30</span>)
    <span class="hljs-keyword">return</span> result

0 人点赞