MukeshKapoor25 commited on
Commit
a279b4f
·
1 Parent(s): 1f3f77e

feat(redis): Add REDIS_URL configuration for flexible Redis connection

Browse files
Files changed (3) hide show
  1. .env.example +1 -0
  2. app/cache.py +17 -9
  3. app/core/config.py +1 -0
.env.example CHANGED
@@ -26,6 +26,7 @@ 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_HOST=localhost
30
  REDIS_PORT=6379
31
  REDIS_USERNAME=default
 
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=redis://default:your-redis-password@localhost:6379/0
30
  REDIS_HOST=localhost
31
  REDIS_PORT=6379
32
  REDIS_USERNAME=default
app/cache.py CHANGED
@@ -24,15 +24,23 @@ async def connect_to_redis():
24
  "port": settings.REDIS_PORT,
25
  "db": settings.REDIS_DB
26
  })
27
-
28
- redis_client = redis.Redis(
29
- host=settings.REDIS_HOST,
30
- port=settings.REDIS_PORT,
31
- username=settings.REDIS_USERNAME,
32
- password=settings.REDIS_PASSWORD,
33
- db=settings.REDIS_DB,
34
- decode_responses=True
35
- )
 
 
 
 
 
 
 
 
36
 
37
  # Test the connection
38
  await redis_client.ping()
 
24
  "port": settings.REDIS_PORT,
25
  "db": settings.REDIS_DB
26
  })
27
+
28
+ if settings.REDIS_URL:
29
+ redis_client = redis.from_url(
30
+ settings.REDIS_URL,
31
+ decode_responses=True
32
+ )
33
+ else:
34
+ username = settings.REDIS_USERNAME or None
35
+ password = settings.REDIS_PASSWORD or None
36
+ redis_client = redis.Redis(
37
+ host=settings.REDIS_HOST,
38
+ port=settings.REDIS_PORT,
39
+ username=username,
40
+ password=password,
41
+ db=settings.REDIS_DB,
42
+ decode_responses=True
43
+ )
44
 
45
  # Test the connection
46
  await redis_client.ping()
app/core/config.py CHANGED
@@ -80,6 +80,7 @@ class Settings(BaseSettings):
80
  return self
81
 
82
  # Redis Configuration
 
83
  REDIS_HOST: str = os.getenv("REDIS_HOST", "localhost")
84
  REDIS_PORT: int = int(os.getenv("REDIS_PORT", "6379"))
85
  REDIS_USERNAME: Optional[str] = os.getenv("REDIS_USERNAME")
 
80
  return self
81
 
82
  # Redis Configuration
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")