| | import redis |
| | import logging |
| | from typing import Optional |
| | from redis import ConnectionPool |
| |
|
| | logger = logging.getLogger(__name__) |
| |
|
| | class RedisClient: |
| | """Hardcoded Redis client with non-SSL configuration and connection pooling""" |
| | |
| | _instance = None |
| | _redis_client = None |
| |
|
| | def __new__(cls): |
| | if cls._instance is None: |
| | cls._instance = super(RedisClient, cls).__new__(cls) |
| | return cls._instance |
| |
|
| | def __init__(self): |
| | if not hasattr(self, '_initialized'): |
| | self._initialized = True |
| | self.pool = None |
| | self._connect() |
| |
|
| | def _connect(self): |
| | """Establish Redis connection without SSL using connection pooling""" |
| | logger.info("=== Redis Connection (Non-SSL) ===") |
| | host = 'redis-16717.c85.us-east-1-2.ec2.redns.redis-cloud.com' |
| | port = 16717 |
| | username = "default" |
| | password = "bNQGmfkB2fRo4KrT3UXwhAUEUmgDClx7" |
| | |
| | logger.info(f"Host: {host}") |
| | logger.info(f"Port: {port}") |
| | logger.info(f"Username: {username}") |
| | logger.info("Password: [REDACTED]") |
| | logger.info("SSL: Disabled") |
| | logger.info("==============================") |
| |
|
| | try: |
| | logger.info("Creating Redis connection pool...") |
| | self.pool = ConnectionPool( |
| | host=host, |
| | port=port, |
| | username=username, |
| | password=password, |
| | decode_responses=True, |
| | max_connections=20, |
| | socket_connect_timeout=15, |
| | socket_timeout=15, |
| | health_check_interval=30, |
| | retry_on_timeout=True |
| | ) |
| | |
| | logger.info("Creating Redis client with connection pool...") |
| | self._redis_client = redis.Redis(connection_pool=self.pool) |
| | |
| | logger.info("Attempting to ping Redis...") |
| | result = self._redis_client.ping() |
| | logger.info(f"β
Ping successful: {result}") |
| | |
| | |
| | logger.info("Testing set/get operations...") |
| | self._redis_client.set('connection_test_key', 'connection_test_value') |
| | value = self._redis_client.get('connection_test_key') |
| | self._redis_client.delete('connection_test_key') |
| | |
| | if value == 'connection_test_value': |
| | logger.info("β
Set/Get test successful!") |
| | logger.info("π Redis connection established successfully without SSL!") |
| | else: |
| | logger.warning("β Set/Get test failed") |
| | |
| | except Exception as e: |
| | logger.error(f"β Redis connection failed: {e}") |
| | logger.error(f"Error type: {type(e).__name__}") |
| | import traceback |
| | logger.error(f"Traceback: {traceback.format_exc()}") |
| | self._redis_client = None |
| |
|
| | def get_client(self) -> Optional[redis.Redis]: |
| | """Get Redis client instance""" |
| | return self._redis_client |
| |
|
| | def is_healthy(self) -> bool: |
| | """Check if Redis connection is healthy""" |
| | if not self._redis_client: |
| | return False |
| | try: |
| | self._redis_client.ping() |
| | return True |
| | except Exception as e: |
| | logger.error(f"Redis health check failed: {e}") |
| | return False |
| |
|
| | def reconnect(self): |
| | """Reconnect to Redis""" |
| | self._connect() |
| |
|
| | |
| | redis_client = RedisClient() |
| |
|