add redis client
This commit is contained in:
parent
9b291d97ea
commit
4bee747f00
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -28,3 +28,4 @@ binance-sdk-spot
|
|||
telethon
|
||||
fastapi
|
||||
uvicorn[standard]
|
||||
redis[hiredis]
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)),
|
||||
},
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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()
|
||||
Loading…
Reference in New Issue