"""草稿服务(Redis 存储,零迁移)""" import redis.asyncio as aioredis from app.config import settings class DraftService: def __init__(self): self._redis = None async def _get_redis(self): if self._redis is None: self._redis = aioredis.from_url(settings.REDIS_URL, decode_responses=True) return self._redis async def get(self, user_id: str, conv_id: str) -> str: r = await self._get_redis() return await r.get(f"draft:{user_id}:{conv_id}") or "" async def set(self, user_id: str, conv_id: str, text: str): r = await self._get_redis() key = f"draft:{user_id}:{conv_id}" if text: await r.set(key, text, ex=30 * 86400) # 30 天 TTL else: await r.delete(key)