a
This commit is contained in:
parent
6aca7f9f73
commit
46cb1f91d3
|
|
@ -198,8 +198,10 @@ class ConfigManager:
|
|||
# 先尝试从Redis加载所有配置
|
||||
if self._redis_connected and self._redis_client:
|
||||
try:
|
||||
# 测试连接是否真正可用
|
||||
self._redis_client.ping()
|
||||
redis_configs = self._redis_client.hgetall('trading_config')
|
||||
if redis_configs:
|
||||
if redis_configs and len(redis_configs) > 0:
|
||||
# 解析Redis中的配置
|
||||
for key, value_str in redis_configs.items():
|
||||
try:
|
||||
|
|
@ -211,10 +213,18 @@ class ConfigManager:
|
|||
self._cache[key] = value
|
||||
logger.info(f"从Redis加载了 {len(self._cache)} 个配置项")
|
||||
return
|
||||
else:
|
||||
# Redis中没有配置,需要从数据库加载并同步到Redis
|
||||
logger.debug("Redis中没有配置数据,从数据库加载并同步到Redis")
|
||||
except Exception as e:
|
||||
logger.debug(f"从Redis加载配置失败: {e},回退到数据库")
|
||||
# 连接失败时,标记为未连接
|
||||
try:
|
||||
self._redis_client.ping()
|
||||
except:
|
||||
self._redis_connected = False
|
||||
|
||||
# 从数据库加载配置
|
||||
# 从数据库加载配置(仅在Redis不可用或Redis中没有数据时)
|
||||
configs = TradingConfig.get_all()
|
||||
for config in configs:
|
||||
key = config['config_key']
|
||||
|
|
@ -283,11 +293,23 @@ class ConfigManager:
|
|||
|
||||
def reload_from_redis(self):
|
||||
"""强制从Redis重新加载配置(用于trading_system即时获取最新配置)"""
|
||||
self._cache = {}
|
||||
if self._redis_connected and self._redis_client:
|
||||
# 如果Redis不可用,不重新加载,保持现有缓存
|
||||
if not self._redis_connected or not self._redis_client:
|
||||
logger.debug("Redis未连接,跳过从Redis重新加载,保持现有缓存")
|
||||
return
|
||||
|
||||
try:
|
||||
# 测试连接是否真正可用
|
||||
self._redis_client.ping()
|
||||
except Exception as e:
|
||||
logger.debug(f"Redis连接不可用: {e},跳过从Redis重新加载")
|
||||
self._redis_connected = False
|
||||
return
|
||||
|
||||
try:
|
||||
redis_configs = self._redis_client.hgetall('trading_config')
|
||||
if redis_configs:
|
||||
if redis_configs and len(redis_configs) > 0:
|
||||
self._cache = {} # 清空缓存
|
||||
for key, value_str in redis_configs.items():
|
||||
try:
|
||||
value = json.loads(value_str)
|
||||
|
|
@ -295,12 +317,16 @@ class ConfigManager:
|
|||
value = value_str
|
||||
self._cache[key] = value
|
||||
logger.debug(f"从Redis重新加载了 {len(self._cache)} 个配置项")
|
||||
return
|
||||
else:
|
||||
# Redis中没有配置,但不回退到数据库(避免频繁从数据库加载)
|
||||
logger.debug("Redis中没有配置数据,保持现有缓存")
|
||||
except Exception as e:
|
||||
logger.debug(f"从Redis重新加载配置失败: {e}")
|
||||
|
||||
# 如果Redis失败,回退到数据库
|
||||
self._load_from_db()
|
||||
logger.debug(f"从Redis重新加载配置失败: {e},保持现有缓存")
|
||||
# 连接失败时,标记为未连接
|
||||
try:
|
||||
self._redis_client.ping()
|
||||
except:
|
||||
self._redis_connected = False
|
||||
|
||||
def get_trading_config(self):
|
||||
"""获取交易配置字典(兼容原有config.py的TRADING_CONFIG)"""
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user