Flask-cache
 
 
 目录
 - Flask-cache
 - 基本使用
 - 配置可用参数
 - SimpleCache
 - NullCache
 - FileSystemCache
 - RedisCache
 - RedisSentinelCache
 - RedisClusterCache
 - MemcachedCache
 - SASLMemcachedCache
 - UWSGICache
 
 
 
 
  
 
Flask-Cache是一个强大的缓存库,为基于Flask的应用提供了简单易用的API和多种缓存策略
 
安装
 
pip install Flask-Caching
 
基本使用
 
from flask import Flask, jsonify
from flask_caching import Cache, SimpleCacheapp = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})
@app.route('/')
def index():cache.set('name', '张三')return 'ok'
@app.route('/home')
def home():return cache.get('name')if __name__ == '__main__':app.run(debug=True)
 
配置可用参数
 
SimpleCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_DEFAULT_TIMEOUT | 默认超时时间,单位为秒 | 
| CACHE_THRESHOLD | 最大缓存数 | 
| CACHE_IGNORE_ERRORS | 是否忽略删除过程中的错误 | 
 
NullCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_NO_NULL_WARNING | 不会抛出警告信息 | 
| CACHE_DEFAULT_TIMEOUT | 默认超时时间,单位为秒 | 
 
FileSystemCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_DEFAULT_TIMEOUT | 默认超时时间,单位为秒 | 
| CACHE_IGNORE_ERRORS | 是否忽略删除过程中的错误 | 
| CACHE_DIR | 存储缓存的目录 | 
| CACHE_THRESHOLD | 最大缓存数 | 
| CACHE_OPTIONS | 缓存类实例化时传递的可选字典 | 
 
RedisCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_DEFAULT_TIMEOUT | 默认超时时间,单位为秒 | 
| CACHE_KEY_PREFIX | 缓存键的统一前缀 | 
| CACHE_OPTIONS | 缓存类实例化时传递的可选字典 | 
| CACHE_REDIS_HOST | Redis 服务器主机 | 
| CACHE_REDIS_PORT | Redis 服务器端口,默认为 6379 | 
| CACHE_REDIS_PASSWORD | Redis 服务器密码 | 
| CACHE_REDIS_DB | Redis 数据库索引,默认为 0 | 
| CACHE_REDIS_URL | 连接到 Redis 服务器的 URL | 
 
RedisSentinelCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_KEY_PREFIX | 缓存键的统一前缀 | 
| CACHE_REDIS_SENTINELS | Redis哨兵地址列表或元组 | 
| CACHE_REDIS_SENTINEL_MASTER | 哨兵配置中的主服务器名称 | 
| CACHE_REDIS_PASSWORD | Redis 服务器密码 | 
| CACHE_REDIS_DB | Redis 数据库索引,默认为 0 | 
 
RedisClusterCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_KEY_PREFIX | 缓存键的统一前缀 | 
| CACHE_REDIS_CLUSTER | 以逗号分隔的 Redis 集群节点地址 | 
| CACHE_REDIS_PASSWORD | Redis 服务器密码 | 
 
MemcachedCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_DEFAULT_TIMEOUT | 默认超时时间,单位为秒 | 
| CACHE_KEY_PREFIX | 缓存键的统一前缀 | 
| CACHE_MEMCACHED_SERVERS | 服务器地址的列表或元组 | 
 
SASLMemcachedCache
 
| 配置参数 | 说明 | 
|---|
| CACHE_DEFAULT_TIMEOUT | 默认超时时间,单位为秒 | 
| CACHE_KEY_PREFIX | 缓存键的统一前缀 | 
| CACHE_OPTIONS | 缓存类实例化时传递的可选字典 | 
| CACHE_MEMCACHED_SERVERS | 服务器地址的列表或元组 | 
| CACHE_MEMCACHED_USERNAME | SASL身份验证用户名 | 
| CACHE_MEMCACHED_PASSWORD | SASL身份验证密码 | 
 
UWSGICache
 
| 配置参数 | 说明 | 
|---|
| CACHE_UWSGI_NAME | 连接的 uwsgi 缓存实例的名称 |