Skip to content

Cache - 配置指南

阅读路径

🟡 运维:README → configuration → troubleshooting → best-practices

初始化与生命周期

初始化

python
from FQBase.Cache import init_cache_adapter, create_cache

# 方式1: 从环境变量初始化
init_cache_adapter()

# 方式2: 手动创建
from FQBase.Cache import RedisCacheAdapter, CacheConfig
config = CacheConfig(cache_type='redis', redis_host='localhost')
cache = RedisCacheAdapter(config)

生命周期

阶段方法说明
初始化init_cache_adapter()从环境变量初始化全局适配器
获取get_cache_adapter()获取全局适配器
设置set_cache_adapter()设置全局适配器
失效invalidate_cache()使缓存失效

配置选项

CacheConfig

选项类型默认值描述
cache_typestrmemory缓存类型:memory/redis/mongo
ttl_defaultint0默认 TTL(秒),0 表示永不过期

RedisConfigProtocol

选项类型默认值描述
redis_hoststrlocalhostRedis 主机
redis_portint6379Redis 端口
redis_passwordstrNoneRedis 密码
redis_dbint0Redis 数据库编号
redis_max_connectionsint50最大连接数
redis_timeoutint10连接超时(秒)

MongoConfigProtocol

选项类型默认值描述
mongo_uristrmongodb://localhost:27017MongoDB URI
mongo_databasestrcache数据库名
mongo_collectionstrcache_data集合名
mongo_ttlint3600默认 TTL(秒)

环境变量

变量类型默认值描述
CACHE_TYPEstrmemory缓存类型:memory/redis/mongo
REDIS_HOSTstrlocalhostRedis 主机
REDIS_PORTint6379Redis 端口
REDIS_PASSWORDstrNoneRedis 密码
REDIS_DBint0Redis 数据库编号

配置优先级

  1. 环境变量 → 2. 显式传入配置 → 3. 默认值

配置示例

最小配置(LocalCache)

bash
# .env
CACHE_TYPE=memory
python
from FQBase.Cache import create_cache
cache = create_cache()

Redis 配置

bash
# .env
CACHE_TYPE=redis
REDIS_HOST=redis.example.com
REDIS_PORT=6379
REDIS_PASSWORD=secret
python
from FQBase.Cache import create_cache
cache = create_cache()

MongoDB 配置

bash
# .env
CACHE_TYPE=mongo
MONGO_URI=mongodb://mongo.example.com:27017
MONGO_DATABASE=my_cache
python
from FQBase.Cache import create_cache
cache = create_cache()

完整配置

python
from FQBase.Cache import RedisCacheAdapter, CacheConfig

config = CacheConfig(
    cache_type='redis',
    redis_host='redis.example.com',
    redis_port=6379,
    redis_password='secret',
    redis_max_connections=100,
    redis_timeout=30,
)
cache = RedisCacheAdapter(config)

动态配置

切换缓存后端

python
from FQBase.Cache import set_cache_adapter, LocalCache

# 切换到 LocalCache
set_cache_adapter(LocalCache(name="temp"))

# 切换回 Redis
from FQBase.Cache import RedisCacheAdapter
set_cache_adapter(RedisCacheAdapter())

监听配置变更

python
from FQBase.Cache import get_cache_adapter

adapter = get_cache_adapter()
adapter.set("config:last_update", str(time.time()))

相关文档