This commit is contained in:
tt-P607
2025-12-03 17:02:39 +08:00
16 changed files with 597 additions and 65 deletions

View File

@@ -387,8 +387,36 @@ class StreamToolHistoryManager:
return result
# 全局管理器字典按chat_id索引
# 内存优化:全局管理器字典按chat_id索引,添加 LRU 淘汰
_stream_managers: dict[str, StreamToolHistoryManager] = {}
_stream_managers_last_used: dict[str, float] = {} # 记录最后使用时间
_STREAM_MANAGERS_MAX_SIZE = 100 # 最大保留数量
def _evict_old_stream_managers() -> None:
"""内存优化:淘汰最久未使用的 stream manager"""
import time
if len(_stream_managers) < _STREAM_MANAGERS_MAX_SIZE:
return
# 按最后使用时间排序,淘汰最旧的 20%
evict_count = max(1, len(_stream_managers) // 5)
sorted_by_time = sorted(
_stream_managers_last_used.items(),
key=lambda x: x[1]
)
evicted = []
for chat_id, _ in sorted_by_time[:evict_count]:
if chat_id in _stream_managers:
del _stream_managers[chat_id]
if chat_id in _stream_managers_last_used:
del _stream_managers_last_used[chat_id]
evicted.append(chat_id)
if evicted:
logger.info(f"🔧 StreamToolHistoryManager LRU淘汰: 释放了 {len(evicted)} 个不活跃的管理器")
def get_stream_tool_history_manager(chat_id: str) -> StreamToolHistoryManager:
@@ -400,7 +428,14 @@ def get_stream_tool_history_manager(chat_id: str) -> StreamToolHistoryManager:
Returns:
工具历史记录管理器实例
"""
import time
# 🔧 更新最后使用时间
_stream_managers_last_used[chat_id] = time.time()
if chat_id not in _stream_managers:
# 🔧 检查是否需要淘汰
_evict_old_stream_managers()
_stream_managers[chat_id] = StreamToolHistoryManager(chat_id)
return _stream_managers[chat_id]
@@ -413,4 +448,6 @@ def cleanup_stream_manager(chat_id: str) -> None:
"""
if chat_id in _stream_managers:
del _stream_managers[chat_id]
logger.info(f"已清理聊天 {chat_id} 的工具历史记录管理器")
if chat_id in _stream_managers_last_used:
del _stream_managers_last_used[chat_id]
logger.info(f"已清理聊天 {chat_id} 的工具历史记录管理器")

View File

@@ -14,11 +14,19 @@ logger = get_logger("relationship_service")
class RelationshipService:
"""用户关系分服务 - 独立于插件的数据库直接访问层"""
"""用户关系分服务 - 独立于插件的数据库直接访问层
内存优化:添加缓存大小限制和自动过期清理
"""
# 🔧 缓存配置
CACHE_MAX_SIZE = 1000 # 最大缓存用户数
def __init__(self):
self._cache: dict[str, dict] = {} # user_id -> {score, text, last_updated}
self._cache_ttl = 300 # 缓存5分钟
self._last_cleanup = time.time() # 上次清理时间
self._cleanup_interval = 60 # 每60秒清理一次过期条目
async def get_user_relationship_score(self, user_id: str) -> float:
"""
@@ -162,6 +170,9 @@ class RelationshipService:
def _get_from_cache(self, user_id: str) -> dict | None:
"""从缓存获取数据"""
# 🔧 触发定期清理
self._maybe_cleanup_expired()
if user_id in self._cache:
cached_data = self._cache[user_id]
if time.time() - cached_data["last_updated"] < self._cache_ttl:
@@ -173,12 +184,46 @@ class RelationshipService:
def _update_cache(self, user_id: str, score: float, text: str):
"""更新缓存"""
# 🔧 内存优化:检查缓存大小限制
if len(self._cache) >= self.CACHE_MAX_SIZE and user_id not in self._cache:
# 淘汰最旧的 10% 条目
self._evict_oldest_entries()
self._cache[user_id] = {
"score": score,
"text": text,
"last_updated": time.time()
}
def _maybe_cleanup_expired(self):
"""🔧 内存优化:定期清理过期条目"""
now = time.time()
if now - self._last_cleanup < self._cleanup_interval:
return
self._last_cleanup = now
expired_keys = []
for user_id, data in self._cache.items():
if now - data["last_updated"] >= self._cache_ttl:
expired_keys.append(user_id)
for key in expired_keys:
del self._cache[key]
if expired_keys:
logger.debug(f"🔧 relationship_service 清理了 {len(expired_keys)} 个过期缓存条目")
def _evict_oldest_entries(self):
"""🔧 内存优化:淘汰最旧的条目"""
evict_count = max(1, len(self._cache) // 10)
sorted_entries = sorted(
self._cache.items(),
key=lambda x: x[1]["last_updated"]
)
for user_id, _ in sorted_entries[:evict_count]:
del self._cache[user_id]
logger.debug(f"🔧 relationship_service LRU淘汰了 {evict_count} 个缓存条目")
async def _fetch_from_database(self, user_id: str) -> UserRelationships | None:
"""从数据库获取关系数据"""
try: