From c44554f4d417bd251d04b70332e0e4111925f9b0 Mon Sep 17 00:00:00 2001 From: zhangzhanwei Date: Fri, 14 Nov 2025 21:19:49 +0800 Subject: [PATCH] perf: Memory optimization --- apps/application/serializers/common.py | 31 +++++++++++++------------- apps/chat/serializers/chat.py | 5 ++++- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/apps/application/serializers/common.py b/apps/application/serializers/common.py index f38080cd8..fdfa991b0 100644 --- a/apps/application/serializers/common.py +++ b/apps/application/serializers/common.py @@ -258,23 +258,24 @@ class ChatInfo: @staticmethod def map_to_chat_record(chat_record_dict): 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'), - answer_text=chat_record_dict.get('answer_text'), - answer_text_list=chat_record_dict.get('answer_text_list'), - message_tokens=chat_record_dict.get('message_tokens'), - answer_tokens=chat_record_dict.get('answer_tokens'), - const=chat_record_dict.get('const'), - details=chat_record_dict.get('details'), - improve_paragraph_id_list=chat_record_dict.get('improve_paragraph_id_list'), - run_time=chat_record_dict.get('run_time'), - index=chat_record_dict.get('index'), ) + chat_id=chat_record_dict.get('chat_id'), + vote_status=chat_record_dict.get('vote_status'), + problem_text=chat_record_dict.get('problem_text'), + answer_text=chat_record_dict.get('answer_text'), + answer_text_list=chat_record_dict.get('answer_text_list'), + message_tokens=chat_record_dict.get('message_tokens'), + answer_tokens=chat_record_dict.get('answer_tokens'), + const=chat_record_dict.get('const'), + details=chat_record_dict.get('details'), + improve_paragraph_id_list=chat_record_dict.get('improve_paragraph_id_list'), + run_time=chat_record_dict.get('run_time'), + index=chat_record_dict.get('index'), ) def set_cache(self): - 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) + if self.debug: + 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): diff --git a/apps/chat/serializers/chat.py b/apps/chat/serializers/chat.py index bd0ff01aa..28e361292 100644 --- a/apps/chat/serializers/chat.py +++ b/apps/chat/serializers/chat.py @@ -446,7 +446,10 @@ 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 = self.re_open_chat(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() return chat_info def re_open_chat(self, chat_id: str):