mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-26 01:33:05 +00:00
perf: Memory optimization
This commit is contained in:
parent
3ce69d64fb
commit
85f2e9e4a0
|
|
@ -6,6 +6,7 @@
|
|||
@date:2025/6/9 13:42
|
||||
@desc:
|
||||
"""
|
||||
import json
|
||||
from typing import List
|
||||
|
||||
from django.core.cache import cache
|
||||
|
|
@ -19,6 +20,7 @@ from application.models import Application, ChatRecord, Chat, ApplicationVersion
|
|||
from application.serializers.application_chat import ChatCountSerializer
|
||||
from common.constants.cache_version import Cache_Version
|
||||
from common.database_model_manage.database_model_manage import DatabaseModelManage
|
||||
from common.encoder.encoder import SystemEncoder
|
||||
from common.exception.app_exception import ChatException
|
||||
from knowledge.models import Document
|
||||
from models_provider.models import Model
|
||||
|
|
@ -234,7 +236,7 @@ class ChatInfo:
|
|||
'knowledge_id_list': self.knowledge_id_list,
|
||||
'exclude_document_id_list': self.exclude_document_id_list,
|
||||
'application_id': self.application_id,
|
||||
'chat_record_list': [self.chat_record_to_map(c) for c in self.chat_record_list],
|
||||
'chat_record_list': [self.chat_record_to_map(c) for c in self.chat_record_list][-20:],
|
||||
'debug': self.debug
|
||||
}
|
||||
|
||||
|
|
@ -255,7 +257,7 @@ class ChatInfo:
|
|||
|
||||
@staticmethod
|
||||
def map_to_chat_record(chat_record_dict):
|
||||
ChatRecord(id=chat_record_dict.get('id'),
|
||||
return ChatRecord(id=chat_record_dict.get('id'),
|
||||
chat_id=chat_record_dict.get('chat_id'),
|
||||
vote_status=chat_record_dict.get('vote_status'),
|
||||
problem_text=chat_record_dict.get('problem_text'),
|
||||
|
|
@ -270,21 +272,24 @@ class ChatInfo:
|
|||
index=chat_record_dict.get('index'), )
|
||||
|
||||
def set_cache(self):
|
||||
cache.set(Cache_Version.CHAT.get_key(key=self.chat_id), self.to_dict(),
|
||||
cache.set(Cache_Version.CHAT.get_key(key=self.chat_id),json.dumps( self.to_dict(),cls=SystemEncoder),
|
||||
version=Cache_Version.CHAT_INFO.get_version(),
|
||||
timeout=60 * 30)
|
||||
|
||||
@staticmethod
|
||||
def map_to_chat_info(chat_info_dict):
|
||||
return ChatInfo(chat_info_dict.get('chat_id'), chat_info_dict.get('chat_user_id'),
|
||||
chat_info_dict.get('chat_user_type'), chat_info_dict.get('knowledge_id_list'),
|
||||
chat_info_dict.get('exclude_document_id_list'),
|
||||
chat_info_dict.get('application_id'),
|
||||
[ChatInfo.map_to_chat_record(c_r) for c_r in chat_info_dict.get('chat_record_list')])
|
||||
c = ChatInfo(chat_info_dict.get('chat_id'), chat_info_dict.get('chat_user_id'),
|
||||
chat_info_dict.get('chat_user_type'), chat_info_dict.get('knowledge_id_list'),
|
||||
chat_info_dict.get('exclude_document_id_list'),
|
||||
chat_info_dict.get('application_id'),
|
||||
debug=chat_info_dict.get('debug'))
|
||||
c.chat_record_list = [ChatInfo.map_to_chat_record(c_r) for c_r in chat_info_dict.get('chat_record_list')]
|
||||
return c
|
||||
|
||||
@staticmethod
|
||||
def get_cache(chat_id):
|
||||
chat_info_dict = cache.get(Cache_Version.CHAT.get_key(key=chat_id), version=Cache_Version.CHAT_INFO.get_version())
|
||||
chat_info_dict = cache.get(Cache_Version.CHAT.get_key(key=chat_id),
|
||||
version=Cache_Version.CHAT_INFO.get_version())
|
||||
if chat_info_dict:
|
||||
return ChatInfo.map_to_chat_info(chat_info_dict)
|
||||
return ChatInfo.map_to_chat_info(json.loads(chat_info_dict))
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -446,10 +446,7 @@ class ChatSerializers(serializers.Serializer):
|
|||
def get_chat_info(self):
|
||||
self.is_valid(raise_exception=True)
|
||||
chat_id = self.data.get('chat_id')
|
||||
chat_info: ChatInfo = ChatInfo.get_cache(chat_id)
|
||||
if chat_info is None:
|
||||
chat_info: ChatInfo = self.re_open_chat(chat_id)
|
||||
chat_info.set_cache()
|
||||
chat_info: ChatInfo = self.re_open_chat(chat_id)
|
||||
return chat_info
|
||||
|
||||
def re_open_chat(self, chat_id: str):
|
||||
|
|
|
|||
Loading…
Reference in New Issue