一、安装
sudo pip install flask-caching
二、创建
代码语言:javascript复制<span class="hljs-keyword">from</span> flask_caching <span class="hljs-keyword">import</span> Cache
cache = Cache()
<span class="hljs-keyword">from</span> .ext_cache <span class="hljs-keyword">import</span> cache
三、加载
代码语言:javascript复制<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> cache
cache.init_app(app)
四、配置
代码语言:javascript复制<span class="hljs-comment">#缓存redis配置</span>
<span class="hljs-comment">#缓存类型</span>
CACHE_TYPE = <span class="hljs-string">"redis"</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>
CACHE_REDIS_HOST = <span class="hljs-string">"127.0.0.1"</span>
CACHE_REDIS_PORT = <span class="hljs-number">6379</span>
CACHE_REDIS_PASSWORD = <span class="hljs-string">"123456"</span>
CACHE_REDIS_DB = <span class="hljs-number">1</span>
<span class="hljs-comment"># 有密码</span>
<span class="hljs-comment"># CACHE_REDIS_URL = "redis://:123456@127.0.0.1:6379/1"</span>
<span class="hljs-comment"># 无密码</span>
<span class="hljs-comment"># CACHE_REDIS_URL = "redis://:@127.0.0.1:6379/1"</span>
五、使用
使用方式与flask-cache相同,但是解决了cache原码包中的问题