Spaces:
Sleeping
Sleeping
Commit ·
f553d39
1
Parent(s): 483e9e7
feat(redis): Update Redis configuration to support flexible connection handling
Browse files- .env.example +2 -2
- app/cache.py +50 -45
- app/core/config.py +0 -1
.env.example
CHANGED
|
@@ -26,10 +26,10 @@ POSTGRES_CONNECT_BACKOFF_MULTIPLIER=1.5
|
|
| 26 |
DATABASE_URL=postgresql+asyncpg://your-db-user:your-db-password@localhost:5432/cuatrolabs
|
| 27 |
|
| 28 |
# Redis Configuration (for caching and session management)
|
| 29 |
-
REDIS_URL
|
|
|
|
| 30 |
REDIS_HOST=localhost
|
| 31 |
REDIS_PORT=6379
|
| 32 |
-
REDIS_USERNAME=default
|
| 33 |
REDIS_PASSWORD=your-redis-password
|
| 34 |
REDIS_DB=0
|
| 35 |
|
|
|
|
| 26 |
DATABASE_URL=postgresql+asyncpg://your-db-user:your-db-password@localhost:5432/cuatrolabs
|
| 27 |
|
| 28 |
# Redis Configuration (for caching and session management)
|
| 29 |
+
# If REDIS_URL is set, it will be used; otherwise REDIS_HOST/REDIS_PORT are used.
|
| 30 |
+
REDIS_URL=redis://:your-redis-password@localhost:6379/0
|
| 31 |
REDIS_HOST=localhost
|
| 32 |
REDIS_PORT=6379
|
|
|
|
| 33 |
REDIS_PASSWORD=your-redis-password
|
| 34 |
REDIS_DB=0
|
| 35 |
|
app/cache.py
CHANGED
|
@@ -3,7 +3,6 @@ Redis connection and cache instance.
|
|
| 3 |
Provides Redis client for OTP storage and rate limiting.
|
| 4 |
"""
|
| 5 |
import redis.asyncio as redis
|
| 6 |
-
from redis.exceptions import AuthenticationError
|
| 7 |
from insightfy_utils.logging import get_logger
|
| 8 |
from app.core.config import settings
|
| 9 |
|
|
@@ -12,6 +11,51 @@ logger = get_logger(__name__)
|
|
| 12 |
# Redis client instance
|
| 13 |
redis_client: redis.Redis = None
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
async def connect_to_redis():
|
| 17 |
"""
|
|
@@ -20,55 +64,16 @@ async def connect_to_redis():
|
|
| 20 |
"""
|
| 21 |
global redis_client
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
"db": settings.REDIS_DB
|
| 27 |
})
|
| 28 |
|
| 29 |
-
if settings.REDIS_URL:
|
| 30 |
-
redis_client = redis.from_url(
|
| 31 |
-
settings.REDIS_URL,
|
| 32 |
-
decode_responses=True
|
| 33 |
-
)
|
| 34 |
-
else:
|
| 35 |
-
username = settings.REDIS_USERNAME or None
|
| 36 |
-
password = settings.REDIS_PASSWORD or None
|
| 37 |
-
redis_client = redis.Redis(
|
| 38 |
-
host=settings.REDIS_HOST,
|
| 39 |
-
port=settings.REDIS_PORT,
|
| 40 |
-
username=username,
|
| 41 |
-
password=password,
|
| 42 |
-
db=settings.REDIS_DB,
|
| 43 |
-
decode_responses=True
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
# Test the connection
|
| 47 |
-
|
| 48 |
-
await redis_client.ping()
|
| 49 |
-
except AuthenticationError as auth_error:
|
| 50 |
-
if settings.REDIS_URL or not settings.REDIS_USERNAME:
|
| 51 |
-
raise auth_error
|
| 52 |
-
logger.warning(
|
| 53 |
-
"Redis auth failed with username; retrying without username for legacy AUTH",
|
| 54 |
-
extra={
|
| 55 |
-
"host": settings.REDIS_HOST,
|
| 56 |
-
"port": settings.REDIS_PORT,
|
| 57 |
-
"db": settings.REDIS_DB
|
| 58 |
-
}
|
| 59 |
-
)
|
| 60 |
-
redis_client = redis.Redis(
|
| 61 |
-
host=settings.REDIS_HOST,
|
| 62 |
-
port=settings.REDIS_PORT,
|
| 63 |
-
password=settings.REDIS_PASSWORD or None,
|
| 64 |
-
db=settings.REDIS_DB,
|
| 65 |
-
decode_responses=True
|
| 66 |
-
)
|
| 67 |
-
await redis_client.ping()
|
| 68 |
-
|
| 69 |
logger.info("Successfully connected to Redis")
|
| 70 |
except Exception as e:
|
| 71 |
-
logger.error("Failed to
|
| 72 |
raise
|
| 73 |
|
| 74 |
|
|
|
|
| 3 |
Provides Redis client for OTP storage and rate limiting.
|
| 4 |
"""
|
| 5 |
import redis.asyncio as redis
|
|
|
|
| 6 |
from insightfy_utils.logging import get_logger
|
| 7 |
from app.core.config import settings
|
| 8 |
|
|
|
|
| 11 |
# Redis client instance
|
| 12 |
redis_client: redis.Redis = None
|
| 13 |
|
| 14 |
+
# Insightfy-style settings mapping
|
| 15 |
+
CACHE_URL = settings.REDIS_URL
|
| 16 |
+
CACHE_URI = f"{settings.REDIS_HOST}:{settings.REDIS_PORT}"
|
| 17 |
+
CACHE_K = settings.REDIS_PASSWORD
|
| 18 |
+
CACHE_DB = settings.REDIS_DB
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def create_redis_connection(**kwargs) -> redis.Redis:
|
| 22 |
+
"""Create a Redis client with the provided kwargs."""
|
| 23 |
+
return redis.Redis(**kwargs)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# -----------------------------------------------------------------------------
|
| 27 |
+
# Redis client initialization with insightfy patterns
|
| 28 |
+
# -----------------------------------------------------------------------------
|
| 29 |
+
def _redis_from_settings() -> redis.Redis:
|
| 30 |
+
"""Create Redis client from settings using insightfy patterns."""
|
| 31 |
+
if CACHE_URL:
|
| 32 |
+
return redis.from_url(
|
| 33 |
+
CACHE_URL,
|
| 34 |
+
decode_responses=True,
|
| 35 |
+
socket_timeout=5,
|
| 36 |
+
socket_connect_timeout=5,
|
| 37 |
+
retry_on_timeout=True,
|
| 38 |
+
health_check_interval=30,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
host, port_str = CACHE_URI.split(":")
|
| 43 |
+
port = int(port_str)
|
| 44 |
+
except Exception:
|
| 45 |
+
raise ValueError("Invalid CACHE_URI format. Expected 'host:port'.")
|
| 46 |
+
|
| 47 |
+
return create_redis_connection(
|
| 48 |
+
host=host,
|
| 49 |
+
port=port,
|
| 50 |
+
password=CACHE_K or None,
|
| 51 |
+
db=CACHE_DB,
|
| 52 |
+
decode_responses=True,
|
| 53 |
+
socket_timeout=5,
|
| 54 |
+
socket_connect_timeout=5,
|
| 55 |
+
retry_on_timeout=True,
|
| 56 |
+
health_check_interval=30,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
|
| 60 |
async def connect_to_redis():
|
| 61 |
"""
|
|
|
|
| 64 |
"""
|
| 65 |
global redis_client
|
| 66 |
try:
|
| 67 |
+
redis_client = _redis_from_settings()
|
| 68 |
+
logger.info("Redis client initialized with insightfy patterns", extra={
|
| 69 |
+
"service": "insightfy-bms-ms-mhs"
|
|
|
|
| 70 |
})
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# Test the connection
|
| 73 |
+
await redis_client.ping()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
logger.info("Successfully connected to Redis")
|
| 75 |
except Exception as e:
|
| 76 |
+
logger.error("Failed to initialize Redis client", exc_info=e)
|
| 77 |
raise
|
| 78 |
|
| 79 |
|
app/core/config.py
CHANGED
|
@@ -83,7 +83,6 @@ class Settings(BaseSettings):
|
|
| 83 |
REDIS_URL: Optional[str] = os.getenv("REDIS_URL")
|
| 84 |
REDIS_HOST: str = os.getenv("REDIS_HOST", "localhost")
|
| 85 |
REDIS_PORT: int = int(os.getenv("REDIS_PORT", "6379"))
|
| 86 |
-
REDIS_USERNAME: Optional[str] = os.getenv("REDIS_USERNAME")
|
| 87 |
REDIS_PASSWORD: Optional[str] = os.getenv("REDIS_PASSWORD")
|
| 88 |
REDIS_DB: int = int(os.getenv("REDIS_DB", "0"))
|
| 89 |
|
|
|
|
| 83 |
REDIS_URL: Optional[str] = os.getenv("REDIS_URL")
|
| 84 |
REDIS_HOST: str = os.getenv("REDIS_HOST", "localhost")
|
| 85 |
REDIS_PORT: int = int(os.getenv("REDIS_PORT", "6379"))
|
|
|
|
| 86 |
REDIS_PASSWORD: Optional[str] = os.getenv("REDIS_PASSWORD")
|
| 87 |
REDIS_DB: int = int(os.getenv("REDIS_DB", "0"))
|
| 88 |
|