Эх сурвалжийг харах

优化消息推送多个用户根据lang缓存文案

zhangdongming 4 сар өмнө
parent
commit
65350982a5

+ 26 - 8
Service/DevicePushService.py

@@ -207,6 +207,7 @@ class DevicePushService:
 
                 push_kwargs = params['push_kwargs']
                 push_token_list = []
+                msg_cache = {}
                 for up in params['uid_set_push_list']:
                     push_type = up['push_type']
                     lang = up['lang']
@@ -226,14 +227,31 @@ class DevicePushService:
                     if process_token in push_token_list:
                         LOGGING.info("uid:{}, 重复token_val{},".format(uid, token_val))
                         continue
-                    # 发送标题
-                    msg_title = cls.get_msg_title(nickname=params['nickname'])
-                    # 发送内容
-                    msg_text = cls.get_msg_text_v2(channel=params['channel'], n_time=params['n_time'], lang=lang, tz=tz,
-                                                   event_type=params['event_type'], ai_type=params['ai_type'],
-                                                   device_type=params['device_type'], electricity=params['electricity'],
-                                                   dealings_type=params['dealings_type'], event_tag=params['event_tag'],
-                                                   redis_obj=params['redis_obj'])
+                    # 如果当前语言下的消息标题和内容尚未计算,则调用函数生成并存入缓存
+                    if lang not in msg_cache:
+                        # 生成标题
+                        title = cls.get_msg_title(nickname=params['nickname'])
+                        # 生成消息内容
+                        text = cls.get_msg_text_v2(
+                            channel=params['channel'],
+                            n_time=params['n_time'],
+                            lang=lang,
+                            tz=tz,
+                            event_type=params['event_type'],
+                            ai_type=params['ai_type'],
+                            device_type=params['device_type'],
+                            electricity=params['electricity'],
+                            dealings_type=params['dealings_type'],
+                            event_tag=params['event_tag'],
+                            redis_obj=params['redis_obj']
+                        )
+                        msg_cache[lang] = (title, text)
+                    else:
+                        title, text = msg_cache[lang]
+
+                    # 使用缓存获取的消息标题和内容
+                    msg_title = title
+                    msg_text = text
 
                     # 补齐推送参数
                     push_kwargs['appBundleId'] = appBundleId