- Changed Redis port mapping in `docker-compose.yml` from `6379:6379` to `6380:6379` to avoid conflicts with Windows Redis. - Updated `.env.example` to reflect the new Redis URL (`redis://127.0.0.1:6380/0`), ensuring proper configuration for Docker users. - Enhanced Redis client initialization in `database.py` and `redis_gateway.py` to utilize a new `_redis_kwargs` function for improved compatibility with Windows Redis 3.x and Docker Redis 7. - Added a new PowerShell script `start-redis.ps1` to facilitate starting Redis in Docker, providing a seamless setup experience for developers. This update significantly improves the Redis integration, ensuring a smoother development process and better compatibility across environments.
45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
"""MySQL / Redis 连接工厂。"""
|
|
|
|
from functools import lru_cache
|
|
|
|
import redis
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.engine import Engine
|
|
|
|
from app.config.settings import settings
|
|
|
|
|
|
def _mysql_url(database: str) -> str:
|
|
pwd = settings.mysql_password
|
|
auth = f"{settings.mysql_user}:{pwd}" if pwd else settings.mysql_user
|
|
return (
|
|
f"mysql+pymysql://{auth}@{settings.mysql_host}:{settings.mysql_port}"
|
|
f"/{database}?charset=utf8mb4"
|
|
)
|
|
|
|
|
|
@lru_cache
|
|
def get_agent_engine() -> Engine:
|
|
return create_engine(_mysql_url(settings.mysql_database), pool_pre_ping=True)
|
|
|
|
|
|
@lru_cache
|
|
def get_core_engine() -> Engine:
|
|
return create_engine(_mysql_url(settings.mysql_core_database), pool_pre_ping=True)
|
|
|
|
|
|
def _redis_kwargs() -> dict:
|
|
"""RESP2:兼容 Windows Redis 3.x(无 HELLO);Redis 7 Docker 同样可用。"""
|
|
return {
|
|
"decode_responses": True,
|
|
"protocol": 2,
|
|
"socket_timeout": 5,
|
|
"socket_connect_timeout": 3,
|
|
}
|
|
|
|
|
|
@lru_cache
|
|
def get_redis_client() -> redis.Redis:
|
|
"""Redis 单例客户端(客服线 VisitorMemory / ProfileHotCache 用)。"""
|
|
return redis.from_url(settings.redis_url, **_redis_kwargs())
|