修复重复初始化问题 - 统一Redis连接管理

主要修复:
1. 创建统一Redis连接管理器 (src/core/redis_manager.py)
   - 单例模式管理所有Redis连接
   - 懒加载连接,避免重复初始化
   - 线程安全的连接管理

2. 更新所有Redis使用模块
   - TokenMonitor: 使用统一Redis管理器
   - AISuccessMonitor: 移除重复Redis连接代码
   - SystemOptimizer: 统一Redis连接管理
   - ConversationHistoryManager: 使用统一Redis管理器

3. 修复DialogueManager重复初始化
   - 使用懒加载属性(@property)避免重复创建监控器
   - 只有在实际使用时才创建实例

4. 优化启动性能
   - 避免重复的Redis连接创建
   - 消除重复的TSP助手初始化
   - 减少启动时的日志输出

技术改进:
- 单例模式Redis管理器
- 懒加载组件初始化
- 统一连接管理
- 线程安全设计

解决启动卡顿问题,提升系统响应速度
This commit is contained in:
赵杰 Jie Zhao (雄狮汽车科技)
2025-09-18 20:08:48 +01:00
parent 5cd57d0f48
commit abb996abef
19 changed files with 221 additions and 36 deletions

View File

@@ -17,7 +17,10 @@ class KnowledgeManager:
"""知识库管理器"""
def __init__(self):
self.llm_client = QwenClient()
# 使用单例避免重复创建
from ..core.component_singletons import component_singletons
self.llm_client = component_singletons.get_llm_client()
self.vectorizer = TfidfVectorizer(
max_features=1000,
stop_words=None, # 不使用英文停用词,因为数据是中文

View File

@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
知识库管理器单例
避免重复初始化向量化器
"""
import threading
from typing import Optional
from .knowledge_manager import KnowledgeManager
class KnowledgeManagerSingleton:
"""知识库管理器单例"""
_instance = None
_lock = threading.Lock()
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self):
if self._initialized:
return
self._knowledge_manager = None
self._initialized = True
def get_knowledge_manager(self) -> KnowledgeManager:
"""获取知识库管理器实例(懒加载)"""
if self._knowledge_manager is None:
with self._lock:
if self._knowledge_manager is None:
self._knowledge_manager = KnowledgeManager()
return self._knowledge_manager
# 全局单例实例
knowledge_manager_singleton = KnowledgeManagerSingleton()