File size: 3,288 Bytes
45df059 ec5a582 78e01dd 45df059 34a92ea 45df059 34a92ea 45df059 34a92ea 45df059 ec5a582 6c0af85 ec5a582 3371ef4 45df059 ec5a582 45df059 6c0af85 45df059 34a92ea 4f1a78a ec5a582 45df059 b283a26 ec5a582 6c0af85 ec5a582 b283a26 ec5a582 45df059 ec5a582 6c0af85 ec5a582 34a92ea 45df059 34a92ea 45df059 6c0af85 45df059 34a92ea 45df059 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import redis
import logging
from typing import Optional
logger = logging.getLogger(__name__)
class RedisClient:
"""Hardcoded Redis client with non-SSL configuration"""
_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._connect()
def _connect(self):
"""Establish Redis connection without SSL"""
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 client without SSL...")
self._redis_client = redis.Redis(
host=host,
port=port,
username=username,
password=password,
decode_responses=True,
socket_connect_timeout=15,
socket_timeout=15,
health_check_interval=30,
retry_on_timeout=True
# NO SSL PARAMETERS
)
logger.info("Attempting to ping Redis...")
result = self._redis_client.ping()
logger.info(f"β
Ping successful: {result}")
# Test set/get to ensure full functionality
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()
# Global Redis client instance
redis_client = RedisClient()
|