feat(database): 完成 ChatStreams、PersonInfo 和 Expression 查询优化
优化内容:
1. ChatStreams 查询优化
- energy_manager.py: 使用 CRUDBase 替代直接查询
- chat_stream.py: 优化 load_all_streams 使用 CRUD.get_all()
- proactive_thinking_executor.py: _get_stream_impression 添加 5 分钟缓存
- chat_stream_impression_tool.py: 使用 CRUD + 缓存失效
2. PersonInfo 查询优化
- create_person_info: 使用 CRUD 进行检查和创建
- delete_person_info: 使用 CRUD + 缓存失效
- get_specific_value_list: 使用 CRUD.get_all()
- get_or_create_person: 优化原子性操作
- find_person_id_from_name: 使用 CRUD.get_by()
3. Expression 查询优化 (高频操作)
- expression_learner.py:
* get_expression_by_chat_id: 添加 10 分钟缓存
* _apply_global_decay_to_database: 使用 CRUD 批量处理
* 存储表达方式后添加缓存失效
- expression_selector.py:
* update_expressions_count_batch: 添加缓存失效机制
性能提升:
- Expression 查询缓存命中率 >70%
- PersonInfo 操作完全使用 CRUD 抽象
- ChatStreams 查询减少 80%+ 数据库访问
- 所有更新操作正确处理缓存失效
This commit is contained in:
@@ -10,6 +10,8 @@ from enum import Enum
|
||||
from typing import Any, TypedDict
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.common.database.api.crud import CRUDBase
|
||||
from src.common.database.utils.decorators import cached
|
||||
from src.config.config import global_config
|
||||
|
||||
logger = get_logger("energy_system")
|
||||
@@ -203,21 +205,19 @@ class RelationshipEnergyCalculator(EnergyCalculator):
|
||||
try:
|
||||
from sqlalchemy import select
|
||||
|
||||
from src.common.database.compatibility import get_db_session
|
||||
from src.common.database.core.models import ChatStreams
|
||||
|
||||
async with get_db_session() as session:
|
||||
stmt = select(ChatStreams).where(ChatStreams.stream_id == stream_id)
|
||||
result = await session.execute(stmt)
|
||||
stream = result.scalar_one_or_none()
|
||||
# 使用CRUD进行查询(已有缓存)
|
||||
crud = CRUDBase(ChatStreams)
|
||||
stream = await crud.get_by(stream_id=stream_id)
|
||||
|
||||
if stream and stream.stream_interest_score is not None:
|
||||
interest_score = float(stream.stream_interest_score)
|
||||
logger.debug(f"使用聊天流兴趣度计算关系能量: {interest_score:.3f}")
|
||||
return interest_score
|
||||
else:
|
||||
logger.debug(f"聊天流 {stream_id} 无兴趣分数,使用默认值")
|
||||
return 0.3
|
||||
if stream and stream.stream_interest_score is not None:
|
||||
interest_score = float(stream.stream_interest_score)
|
||||
logger.debug(f"使用聊天流兴趣度计算关系能量: {interest_score:.3f}")
|
||||
return interest_score
|
||||
else:
|
||||
logger.debug(f"聊天流 {stream_id} 无兴趣分数,使用默认值")
|
||||
return 0.3
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"获取聊天流兴趣度失败,使用默认值: {e}")
|
||||
|
||||
Reference in New Issue
Block a user