fad458b2a7
- UIUX 文档:填充 19 个缺口(多主体画像/健康度/AI+看板/增长域/洞察域/创始人端/OODA/助推/商密) - UIUX 文档:插入 6 个新章节(十四~十九),旧章节重编号为二十~三十一,更新目录和交叉引用 - 作业指导书 x5:导航改为 6 域分组,新增 Context Bar/工作模式/Insight Rail/决策线程/多工作区等 UI 概念 - 新建 docs/2-task-uiux.md:50 个代码落地开发任务,按 P0-P6 分优先级 + 8 Sprint 规划 - 后端/前端:大量新增模型、路由、组件(来自之前 Phase 开发)
48 lines
1.1 KiB
Python
48 lines
1.1 KiB
Python
"""Redis 连接与缓存工具。"""
|
|
|
|
import json
|
|
import logging
|
|
from typing import Any
|
|
|
|
from app.core.config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
try:
|
|
import redis.asyncio as redis
|
|
_redis_client = redis.from_url(settings.redis_url, decode_responses=True)
|
|
except ImportError:
|
|
_redis_client = None
|
|
logger.warning("redis 未安装,缓存功能不可用")
|
|
|
|
|
|
async def cache_get(key: str) -> Any | None:
|
|
"""从 Redis 获取缓存。"""
|
|
if not _redis_client:
|
|
return None
|
|
try:
|
|
data = await _redis_client.get(key)
|
|
return json.loads(data) if data else None
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
async def cache_set(key: str, value: Any, ttl: int = 300) -> None:
|
|
"""设置 Redis 缓存。"""
|
|
if not _redis_client:
|
|
return
|
|
try:
|
|
await _redis_client.setex(key, ttl, json.dumps(value, default=str))
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
async def cache_delete(key: str) -> None:
|
|
"""删除 Redis 缓存。"""
|
|
if not _redis_client:
|
|
return
|
|
try:
|
|
await _redis_client.delete(key)
|
|
except Exception:
|
|
pass
|