From 4bee747f00b93f7fa4b2bf06722930b46f6d38ac Mon Sep 17 00:00:00 2001 From: mhmmdjafarg Date: Tue, 30 Dec 2025 17:40:22 +0700 Subject: [PATCH] add redis client --- .env.example | 5 ++-- requirements.txt | 1 + tradingagents/dataflows/bybit.py | 2 +- tradingagents/default_config.py | 10 ++++++-- tradingagents/external/redis/client.py | 34 ++++++++++++++++++++++++++ tradingagents/external/redis/repo.py | 18 ++++++++++++++ 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tradingagents/external/redis/client.py create mode 100644 tradingagents/external/redis/repo.py diff --git a/.env.example b/.env.example index e5dfe265..f5dce8a3 100644 --- a/.env.example +++ b/.env.example @@ -21,6 +21,7 @@ APP_HOST=localhost APP_PORT=8000 # Redis -REDIS_HOST=redis +REDIS_HOST=localhost REDIS_PORT=6379 -REDIS_PASSWORD=password +REDIS_PASSWORD=default-password +REDIS_DB=0 diff --git a/requirements.txt b/requirements.txt index 57be465a..d5ca8e83 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,3 +28,4 @@ binance-sdk-spot telethon fastapi uvicorn[standard] +redis[hiredis] \ No newline at end of file diff --git a/tradingagents/dataflows/bybit.py b/tradingagents/dataflows/bybit.py index d137001d..54997fdd 100644 --- a/tradingagents/dataflows/bybit.py +++ b/tradingagents/dataflows/bybit.py @@ -6,7 +6,7 @@ from typing import Dict, Optional from urllib.parse import urlencode import requests -from .config import get_config +from tradingagents.dataflows.config import get_config def bybit_v5_request(method: str, path: str, params: Optional[Dict] = None, body: Optional[Dict] = None) -> Dict: diff --git a/tradingagents/default_config.py b/tradingagents/default_config.py index d9e891c6..5151360d 100644 --- a/tradingagents/default_config.py +++ b/tradingagents/default_config.py @@ -44,5 +44,11 @@ DEFAULT_CONFIG = { "TELEGRAM_API_ID": os.getenv("TELEGRAM_API_ID", ""), "TELEGRAM_API_HASH": os.getenv("TELEGRAM_API_HASH", ""), "TELEGRAM_SESSION_NAME": os.getenv("TELEGRAM_SESSION_NAME", ""), - } -} + }, + "redis": { + "REDIS_HOST": os.getenv("REDIS_HOST", "localhost"), + "REDIS_PORT": int(os.getenv("REDIS_PORT", 6379)), + "REDIS_PASSWORD": os.getenv("REDIS_PASSWORD", "defaultpassword"), + "REDIS_DB": int(os.getenv("REDIS_DB", 0)), + }, +} \ No newline at end of file diff --git a/tradingagents/external/redis/client.py b/tradingagents/external/redis/client.py new file mode 100644 index 00000000..2d9c6299 --- /dev/null +++ b/tradingagents/external/redis/client.py @@ -0,0 +1,34 @@ +from redis import Redis, ConnectionPool +from redis.backoff import ExponentialBackoff +from redis.retry import Retry +from tradingagents.dataflows.config import get_config + +_client = None + +def get_redis_client() -> Redis: + """Get or create Redis client with lazy initialization.""" + global _client + if _client is None: + try: + config = get_config() + retry = Retry(ExponentialBackoff(), retries=5) + + pool = ConnectionPool( + host=config["redis"]["REDIS_HOST"], + port=config["redis"]["REDIS_PORT"], + password=config["redis"]["REDIS_PASSWORD"], + db=config["redis"]["REDIS_DB"], + decode_responses=True, + socket_connect_timeout=5, + socket_timeout=5, + health_check_interval=10, + retry=retry, + ) + print("INFO: Initializing Redis client") + _client = Redis(connection_pool=pool) + print("INFO: Redis client initialized successfully") + except Exception as e: + print(f"ERROR: Failed to initialize Redis client: {e}") + raise + + return _client diff --git a/tradingagents/external/redis/repo.py b/tradingagents/external/redis/repo.py new file mode 100644 index 00000000..754919bd --- /dev/null +++ b/tradingagents/external/redis/repo.py @@ -0,0 +1,18 @@ +from tradingagents.external.redis.client import get_redis_client + +redis = get_redis_client() + +class RedisRepo: + def get(self, key: str): + return redis.get(key) + + def set(self, key: str, value: str, ex: int | None = None): + return redis.set(key, value, ex=ex) + + def delete(self, key: str): + return redis.delete(key) + + def exists(self, key: str) -> bool: + return redis.exists(key) == 1 + +redis_repo = RedisRepo()