优化缓存条目大小估算,添加向量存储标记,清理待处理消息逻辑

This commit is contained in:
Windpicker-owo
2025-11-20 12:17:43 +08:00
parent 6f69df9225
commit 03c80a08fb
9 changed files with 173 additions and 20 deletions

View File

@@ -17,7 +17,7 @@ from dataclasses import dataclass
from typing import Any, Generic, TypeVar
from src.common.logger import get_logger
from src.common.memory_utils import estimate_size_smart
from src.common.memory_utils import estimate_cache_item_size
logger = get_logger("cache_manager")
@@ -237,7 +237,7 @@ class LRUCache(Generic[T]):
使用深度递归估算,比 sys.getsizeof() 更准确
"""
try:
return estimate_size_smart(value)
return estimate_cache_item_size(value)
except (TypeError, AttributeError):
# 无法获取大小,返回默认值
return 1024
@@ -345,7 +345,7 @@ class MultiLevelCache:
"""
# 估算数据大小(如果未提供)
if size is None:
size = estimate_size_smart(value)
size = estimate_cache_item_size(value)
# 检查单个条目大小是否超过限制
if size > self.max_item_size_bytes:

View File

@@ -169,6 +169,30 @@ def _estimate_recursive(obj: Any, depth: int, seen: set, sample_large: bool) ->
return size
def estimate_cache_item_size(obj: Any) -> int:
"""
估算缓存条目的大小。
结合深度递归和 pickle 大小,选择更保守的估值,
以避免大量嵌套对象被低估。
"""
try:
smart_size = estimate_size_smart(obj, max_depth=10, sample_large=False)
except Exception:
smart_size = 0
try:
deep_size = get_accurate_size(obj)
except Exception:
deep_size = 0
pickle_size = get_pickle_size(obj)
best = max(smart_size, deep_size, pickle_size)
# 至少返回基础大小,避免 0
return best or sys.getsizeof(obj)
def format_size(size_bytes: int) -> str:
"""
格式化字节数为人类可读的格式