Skip to content

Commit c44554f

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

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

apps/application/serializers/common.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,24 @@ def chat_record_to_map(self, chat_record):
258258
@staticmethod
259259
def map_to_chat_record(chat_record_dict):
260260
return ChatRecord(id=chat_record_dict.get('id'),
261-
chat_id=chat_record_dict.get('chat_id'),
262-
vote_status=chat_record_dict.get('vote_status'),
263-
problem_text=chat_record_dict.get('problem_text'),
264-
answer_text=chat_record_dict.get('answer_text'),
265-
answer_text_list=chat_record_dict.get('answer_text_list'),
266-
message_tokens=chat_record_dict.get('message_tokens'),
267-
answer_tokens=chat_record_dict.get('answer_tokens'),
268-
const=chat_record_dict.get('const'),
269-
details=chat_record_dict.get('details'),
270-
improve_paragraph_id_list=chat_record_dict.get('improve_paragraph_id_list'),
271-
run_time=chat_record_dict.get('run_time'),
272-
index=chat_record_dict.get('index'), )
261+
chat_id=chat_record_dict.get('chat_id'),
262+
vote_status=chat_record_dict.get('vote_status'),
263+
problem_text=chat_record_dict.get('problem_text'),
264+
answer_text=chat_record_dict.get('answer_text'),
265+
answer_text_list=chat_record_dict.get('answer_text_list'),
266+
message_tokens=chat_record_dict.get('message_tokens'),
267+
answer_tokens=chat_record_dict.get('answer_tokens'),
268+
const=chat_record_dict.get('const'),
269+
details=chat_record_dict.get('details'),
270+
improve_paragraph_id_list=chat_record_dict.get('improve_paragraph_id_list'),
271+
run_time=chat_record_dict.get('run_time'),
272+
index=chat_record_dict.get('index'), )
273273

274274
def set_cache(self):
275-
cache.set(Cache_Version.CHAT.get_key(key=self.chat_id),json.dumps( self.to_dict(),cls=SystemEncoder),
276-
version=Cache_Version.CHAT_INFO.get_version(),
277-
timeout=60 * 30)
275+
if self.debug:
276+
cache.set(Cache_Version.CHAT.get_key(key=self.chat_id), json.dumps(self.to_dict(), cls=SystemEncoder),
277+
version=Cache_Version.CHAT_INFO.get_version(),
278+
timeout=60 * 30)
278279

279280
@staticmethod
280281
def map_to_chat_info(chat_info_dict):

apps/chat/serializers/chat.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,10 @@ 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 = self.re_open_chat(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()
450453
return chat_info
451454

452455
def re_open_chat(self, chat_id: str):

0 commit comments

Comments
 (0)