rufffffff
This commit is contained in:
@@ -236,12 +236,12 @@ class ExpressionLearner:
|
||||
"""
|
||||
获取指定chat_id的style和grammar表达方式(带10分钟缓存)
|
||||
返回的每个表达方式字典中都包含了source_id, 用于后续的更新操作
|
||||
|
||||
|
||||
优化: 使用CRUD和缓存,减少数据库访问
|
||||
"""
|
||||
# 使用静态方法以正确处理缓存键
|
||||
return await self._get_expressions_by_chat_id_cached(self.chat_id)
|
||||
|
||||
|
||||
@staticmethod
|
||||
@cached(ttl=600, key_prefix="chat_expressions")
|
||||
async def _get_expressions_by_chat_id_cached(chat_id: str) -> tuple[list[dict[str, float]], list[dict[str, float]]]:
|
||||
@@ -278,7 +278,7 @@ class ExpressionLearner:
|
||||
async def _apply_global_decay_to_database(self, current_time: float) -> None:
|
||||
"""
|
||||
对数据库中的所有表达方式应用全局衰减
|
||||
|
||||
|
||||
优化: 使用CRUD批量处理所有更改,最后统一提交
|
||||
"""
|
||||
try:
|
||||
@@ -288,7 +288,7 @@ class ExpressionLearner:
|
||||
|
||||
updated_count = 0
|
||||
deleted_count = 0
|
||||
|
||||
|
||||
# 需要手动操作的情况下使用session
|
||||
async with get_db_session() as session:
|
||||
# 批量处理所有修改
|
||||
@@ -391,7 +391,7 @@ class ExpressionLearner:
|
||||
current_time = time.time()
|
||||
|
||||
# 存储到数据库 Expression 表
|
||||
crud = CRUDBase(Expression)
|
||||
CRUDBase(Expression)
|
||||
for chat_id, expr_list in chat_dict.items():
|
||||
async with get_db_session() as session:
|
||||
for new_expr in expr_list:
|
||||
@@ -437,10 +437,10 @@ class ExpressionLearner:
|
||||
# 删除count最小的多余表达方式
|
||||
for expr in exprs[: len(exprs) - MAX_EXPRESSION_COUNT]:
|
||||
await session.delete(expr)
|
||||
|
||||
|
||||
# 提交后清除相关缓存
|
||||
await session.commit()
|
||||
|
||||
|
||||
# 清除该chat_id的表达方式缓存
|
||||
from src.common.database.optimization.cache_manager import get_cache
|
||||
from src.common.database.utils.decorators import generate_cache_key
|
||||
|
||||
@@ -9,10 +9,8 @@ from json_repair import repair_json
|
||||
from sqlalchemy import select
|
||||
|
||||
from src.chat.utils.prompt import Prompt, global_prompt_manager
|
||||
from src.common.database.api.crud import CRUDBase
|
||||
from src.common.database.compatibility import get_db_session
|
||||
from src.common.database.core.models import Expression
|
||||
from src.common.database.utils.decorators import cached
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config, model_config
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
@@ -152,7 +150,7 @@ class ExpressionSelector:
|
||||
# sourcery skip: extract-duplicate-method, move-assign
|
||||
# 支持多chat_id合并抽选
|
||||
related_chat_ids = self.get_related_chat_ids(chat_id)
|
||||
|
||||
|
||||
# 使用CRUD查询(由于需要IN条件,使用session)
|
||||
async with get_db_session() as session:
|
||||
# 优化:一次性查询所有相关chat_id的表达方式
|
||||
@@ -224,7 +222,7 @@ class ExpressionSelector:
|
||||
if key not in updates_by_key:
|
||||
updates_by_key[key] = expr
|
||||
affected_chat_ids.add(source_id)
|
||||
|
||||
|
||||
for chat_id, expr_type, situation, style in updates_by_key:
|
||||
async with get_db_session() as session:
|
||||
query = await session.execute(
|
||||
@@ -247,7 +245,7 @@ class ExpressionSelector:
|
||||
f"表达方式激活: 原count={current_count:.3f}, 增量={increment}, 新count={new_count:.3f} in db"
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
|
||||
# 清除所有受影响的chat_id的缓存
|
||||
from src.common.database.optimization.cache_manager import get_cache
|
||||
from src.common.database.utils.decorators import generate_cache_key
|
||||
|
||||
Reference in New Issue
Block a user