32 lines
648 B
Python
32 lines
648 B
Python
|
|
"""Redis 客户端(单例)。用于:
|
||
|
|
- 翻译缓存
|
||
|
|
- 翻译字符配额(月度)
|
||
|
|
- 限流(后续)
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import redis.asyncio as redis_async
|
||
|
|
|
||
|
|
from app.config import settings
|
||
|
|
|
||
|
|
_pool: redis_async.Redis | None = None
|
||
|
|
|
||
|
|
|
||
|
|
def get_redis() -> redis_async.Redis:
|
||
|
|
global _pool
|
||
|
|
if _pool is None:
|
||
|
|
_pool = redis_async.from_url(
|
||
|
|
settings.redis_url,
|
||
|
|
encoding="utf-8",
|
||
|
|
decode_responses=True,
|
||
|
|
max_connections=20,
|
||
|
|
)
|
||
|
|
return _pool
|
||
|
|
|
||
|
|
|
||
|
|
async def close_redis() -> None:
|
||
|
|
global _pool
|
||
|
|
if _pool is not None:
|
||
|
|
await _pool.aclose()
|
||
|
|
_pool = None
|