Explorar o código

查询消息推送OCI对象加入缓存

zhangdongming hai 1 ano
pai
achega
0efe23989b
Modificáronse 1 ficheiros con 42 adicións e 19 borrados
  1. 42 19
      Controller/DetectControllerV2.py

+ 42 - 19
Controller/DetectControllerV2.py

@@ -373,6 +373,7 @@ class DetectControllerViewV2(View):
             )
             oci_eur = OCIObjectStorage('eur')
             oci_us = OCIObjectStorage('us')
+            redis_obj = RedisObject(3)
             # ai消息标识所有组合标签
             ai_all_event_type = EquipmentInfoService.get_all_comb_event_type()
             for equipment_info in equipment_info_qs:
@@ -442,24 +443,34 @@ class DetectControllerViewV2(View):
                 elif equipment_info['is_st'] == 3 or equipment_info['is_st'] == 4:
                     # 列表装载回放时间戳标记
                     equipment_info['img_list'] = []
-                    for i in range(equipment_info['is_st']):
-                        thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
-                        if storage_location == 1:  # 阿里云oss
-                            img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
-                        elif storage_location in [3, 4]:
-                            oci = oci_eur if storage_location == 4 else oci_us
-                            img_url = DetectControllerViewV2.oci_object_url(oci, thumbspng)
-                        else:
-                            params = {'Key': thumbspng}
-                            if region == 1:  # 国外AWS
-                                params['Bucket'] = 'foreignpush'
-                                img_url = aws_s3.generate_presigned_url(
-                                    'get_object', Params=params, ExpiresIn=300)
-                            else:  # 国内AWS
-                                params['Bucket'] = 'push'
-                                img_url = aws_s3_cn.generate_presigned_url(
-                                    'get_object', Params=params, ExpiresIn=300)
-                        equipment_info['img_list'].append(img_url)
+
+                    msg_key = f'{uid}:{channel}:{event_time}'
+                    # OCI三张图 获取缓存临时URL
+                    img_data = DetectControllerViewV2.get_msg_redis_url(redis_obj, msg_key,
+                                                                        equipment_info['is_st'], storage_location)
+                    if img_data:
+                        equipment_info['img_list'] = img_data
+                    else:
+                        for i in range(equipment_info['is_st']):
+                            thumbspng = '{}/{}/{}_{}.jpeg'.format(uid, channel, event_time, i)
+                            if storage_location == 1:  # 阿里云oss
+                                img_url = oss_img_bucket.sign_url('GET', thumbspng, 300)
+                            elif storage_location in [3, 4]:  # 国外OCI云
+                                oci = oci_eur if storage_location == 4 else oci_us
+                                img_url = DetectControllerViewV2.oci_object_url(oci, thumbspng)
+                            else:
+                                params = {'Key': thumbspng}
+                                if region == 1:  # 国外AWS
+                                    params['Bucket'] = 'foreignpush'
+                                    img_url = aws_s3.generate_presigned_url(
+                                        'get_object', Params=params, ExpiresIn=300)
+                                else:  # 国内AWS
+                                    params['Bucket'] = 'push'
+                                    img_url = aws_s3_cn.generate_presigned_url(
+                                        'get_object', Params=params, ExpiresIn=300)
+                            equipment_info['img_list'].append(img_url)
+                        if not img_data and equipment_info['is_st'] == 3 and storage_location in [3, 4]:
+                            DetectControllerViewV2.set_msg_redis_url(redis_obj, msg_key, equipment_info['img_list'])
 
                 if uid in uid_type_dict.keys():
                     equipment_info['uid_type'] = uid_type_dict[uid]['type']
@@ -480,6 +491,18 @@ class DetectControllerViewV2(View):
         except Exception as e:
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
+    @staticmethod
+    def get_msg_redis_url(redis_client, msg_key, is_st, storage_location):
+        if is_st == 3 and storage_location in [3, 4]:
+            img_data = redis_client.get_data(msg_key)
+            if img_data:
+                return json.loads(img_data)
+        return None
+
+    @staticmethod
+    def set_msg_redis_url(redis_client, msg_key, img_list):
+        redis_client.set_data(msg_key, img_list, 580)
+
     @staticmethod
     def oci_object_url(oci, obj_name):
         """
@@ -488,7 +511,7 @@ class DetectControllerViewV2(View):
         @param obj_name: 对象名称
         @return: url
         """
-        time_expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)
+        time_expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=10)
         result = oci.get_preauthenticated_request_url(PUSH_BUCKET, 'ociPush', obj_name, time_expires)
         return result.full_path if result else ''