File size: 1,550 Bytes
268baab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import redis
from threading import Lock
from src.utils.logging import get_logger

logger = get_logger("redis_service")

class RedisService:
    _instance = None
    _init_lock = Lock()

    def __new__(cls, host, port, password, mode):
        if cls._instance is None:
            with cls._init_lock:
                if cls._instance is None:
                    cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self, host, port, password, mode):
        if hasattr(self, '_initialized') and self._initialized:
            return
        
        self._client = None
        self._host = host
        self._port = port
        self._password = password 
        self.mode = mode   
        
        self._connect()

        self._initialized = True

    def _connect(self):
        try:
            logger.info(f"Connecting to Redis at {self._host}:{self._port}...")
            self._client = redis.Redis(
                host=self._host,
                port=self._port,
                password=self._password,
                decode_responses=True,
                socket_connect_timeout=2,
                socket_timeout=2
            )
            self._client.ping()
            logger.info(f"Successfully connected to Redis! {self.mode}")
        except Exception as e:
            logger.error(f"Redis connection failed: {e}")
            self._client = None

    def get_client(self):
        return self._client
    
    def is_connected(self) -> bool:
        return self._client is not None