Skip to content

Commit 85f2e9e

Browse files
committed
perf: Memory optimization
1 parent 3ce69d6 commit 85f2e9e

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

apps/application/serializers/common.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@date:2025/6/9 13:42
77
@desc:
88
"""
9+
import json
910
from typing import List
1011

1112
from django.core.cache import cache
@@ -19,6 +20,7 @@
1920
from application.serializers.application_chat import ChatCountSerializer
2021
from common.constants.cache_version import Cache_Version
2122
from common.database_model_manage.database_model_manage import DatabaseModelManage
23+
from common.encoder.encoder import SystemEncoder
2224
from common.exception.app_exception import ChatException
2325
from knowledge.models import Document
2426
from models_provider.models import Model
@@ -234,7 +236,7 @@ def to_dict(self):
234236
'knowledge_id_list': self.knowledge_id_list,
235237
'exclude_document_id_list': self.exclude_document_id_list,
236238
'application_id': self.application_id,
237-
'chat_record_list': [self.chat_record_to_map(c) for c in self.chat_record_list],
239+
'chat_record_list': [self.chat_record_to_map(c) for c in self.chat_record_list][-20:],
238240
'debug': self.debug
239241
}
240242

@@ -255,7 +257,7 @@ def chat_record_to_map(self, chat_record):
255257

256258
@staticmethod
257259
def map_to_chat_record(chat_record_dict):
258-
ChatRecord(id=chat_record_dict.get('id'),
260+
return ChatRecord(id=chat_record_dict.get('id'),
259261
chat_id=chat_record_dict.get('chat_id'),
260262
vote_status=chat_record_dict.get('vote_status'),
261263
problem_text=chat_record_dict.get('problem_text'),
@@ -270,21 +272,24 @@ def map_to_chat_record(chat_record_dict):
270272
index=chat_record_dict.get('index'), )
271273

272274
def set_cache(self):
273-
cache.set(Cache_Version.CHAT.get_key(key=self.chat_id), self.to_dict(),
275+
cache.set(Cache_Version.CHAT.get_key(key=self.chat_id),json.dumps( self.to_dict(),cls=SystemEncoder),
274276
version=Cache_Version.CHAT_INFO.get_version(),
275277
timeout=60 * 30)
276278

277279
@staticmethod
278280
def map_to_chat_info(chat_info_dict):
279-
return ChatInfo(chat_info_dict.get('chat_id'), chat_info_dict.get('chat_user_id'),
280-
chat_info_dict.get('chat_user_type'), chat_info_dict.get('knowledge_id_list'),
281-
chat_info_dict.get('exclude_document_id_list'),
282-
chat_info_dict.get('application_id'),
283-
[ChatInfo.map_to_chat_record(c_r) for c_r in chat_info_dict.get('chat_record_list')])
281+
c = ChatInfo(chat_info_dict.get('chat_id'), chat_info_dict.get('chat_user_id'),
282+
chat_info_dict.get('chat_user_type'), chat_info_dict.get('knowledge_id_list'),
283+
chat_info_dict.get('exclude_document_id_list'),
284+
chat_info_dict.get('application_id'),
285+
debug=chat_info_dict.get('debug'))
286+
c.chat_record_list = [ChatInfo.map_to_chat_record(c_r) for c_r in chat_info_dict.get('chat_record_list')]
287+
return c
284288

285289
@staticmethod
286290
def get_cache(chat_id):
287-
chat_info_dict = cache.get(Cache_Version.CHAT.get_key(key=chat_id), version=Cache_Version.CHAT_INFO.get_version())
291+
chat_info_dict = cache.get(Cache_Version.CHAT.get_key(key=chat_id),
292+
version=Cache_Version.CHAT_INFO.get_version())
288293
if chat_info_dict:
289-
return ChatInfo.map_to_chat_info(chat_info_dict)
294+
return ChatInfo.map_to_chat_info(json.loads(chat_info_dict))
290295
return None

apps/chat/serializers/chat.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,7 @@ def chat(self, instance: dict, base_to_response: BaseToResponse = SystemToRespon
446446
def get_chat_info(self):
447447
self.is_valid(raise_exception=True)
448448
chat_id = self.data.get('chat_id')
449-
chat_info: ChatInfo = ChatInfo.get_cache(chat_id)
450-
if chat_info is None:
451-
chat_info: ChatInfo = self.re_open_chat(chat_id)
452-
chat_info.set_cache()
449+
chat_info: ChatInfo = self.re_open_chat(chat_id)
453450
return chat_info
454451

455452
def re_open_chat(self, chat_id: str):

0 commit comments

Comments
 (0)