Преглед на файлове

释放redis连接回连接池2

locky преди 1 година
родител
ревизия
10e5e1cb98
променени са 5 файла, в които са добавени 19 реда и са изтрити 3 реда
  1. 2 0
      Controller/Cron/CronTaskController.py
  2. 3 0
      Controller/DetectController.py
  3. 3 1
      Controller/DetectControllerV2.py
  4. 6 0
      Object/RedisObject.py
  5. 5 2
      Service/DevicePushService.py

+ 2 - 0
Controller/Cron/CronTaskController.py

@@ -57,9 +57,11 @@ class CronTaskView(View):
         # 读取缓存的前n条数据批量写入
         redis_r_obj = RedisObject(mode='r')
         equipment_info_redis_list = redis_r_obj.lrange(equipment_info_key, 0, size - 1)
+        redis_r_obj.close()
 
         redis_obj = RedisObject()
         redis_obj.ltrim(equipment_info_key, size, -1)
+        redis_obj.close()
 
         equipment_info_list = []
         for equipment_info in equipment_info_redis_list:

+ 3 - 0
Controller/DetectController.py

@@ -88,6 +88,7 @@ class NotificationView(View):
                 if not uid_push_list:
                     res_data = {'code': 404, 'msg': 'error !'}
                     return JsonResponse(status=200, data=res_data)
+            redis_r_obj.close()
 
             if not uid_push_list:
                 res_data = {'code': 0, 'msg': 'uid_push_list not exist'}
@@ -113,6 +114,8 @@ class NotificationView(View):
             # 旧模式并且没有pkey,重新创建一个
             if not detect_group and not have_pkey:
                 redisObj.set_data(key=pkey, val=1, expire=60)
+            redisObj.close()
+
             auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
             bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'apg')
 

+ 3 - 1
Controller/DetectControllerV2.py

@@ -77,10 +77,12 @@ class NotificationV2View(View):
             req_limiting = '{}_{}_{}_ptl'.format(uid, channel, event_type)
             cache_req_limiting = redis_r_obj.get_data(key=req_limiting)  # 获取请求限流缓存数据
             cache_app_push = redis_r_obj.get_data(key=push_interval)  # 获取APP推送消息时间间隔缓存数据
+            redis_r_obj.close()
             if event_type not in [606, 607]:
                 if cache_req_limiting:  # 限流存在则直接返回
                     return JsonResponse(status=200, data={'code': 0, 'msg': 'Push again in one minute'})
             redis_obj.set_data(key=req_limiting, val=1, expire=60)  # 当缓存不存在限流数据 重新设置一分钟请求一次
+            redis_obj.close()
 
             # 查询uid_push和uid_set数据
             uid_push_qs = DevicePushService.query_uid_push(uid, event_type)
@@ -101,7 +103,7 @@ class NotificationV2View(View):
             if event_type not in [606, 607]:
                 if not cache_app_push:
                     # 缓存APP提醒推送间隔 默认1分钟提醒一次
-                    DevicePushService.cache_push_detect_interval(redis_obj, push_interval, detect_interval,
+                    DevicePushService.cache_push_detect_interval(push_interval, detect_interval,
                                                                  uid_set_push_list[0]['uid_set__new_detect_interval'])
             else:
                 cache_app_push = ''

+ 6 - 0
Object/RedisObject.py

@@ -97,3 +97,9 @@ class RedisObject:
         @return : bool
         """
         return self.CONN.lindex(name, index)
+
+    def close(self):
+        """
+        关闭连接
+        """
+        return self.CONN.close()

+ 5 - 2
Service/DevicePushService.py

@@ -125,10 +125,9 @@ class DevicePushService:
         return qs_list
 
     @staticmethod
-    def cache_push_detect_interval(redis_obj, name, detect_interval, new_detect_interval):
+    def cache_push_detect_interval(name, detect_interval, new_detect_interval):
         """
         缓存设置推送消息的时间间隔
-        @param redis_obj: redis对象
         @param name: redis key
         @param detect_interval: 原推送时间间隔
         @param new_detect_interval: 新推送时间间隔
@@ -138,7 +137,9 @@ class DevicePushService:
             detect_interval = 60 if detect_interval < 60 else detect_interval
         else:  # 国内推送兼容问题,有值并且大于旧消息间隔则使用new_detect_interval
             detect_interval = new_detect_interval if new_detect_interval > detect_interval else detect_interval
+        redis_obj = RedisObject()
         redis_obj.set_data(key=name, val=1, expire=detect_interval - 5)
+        redis_obj.close()
 
     @classmethod
     def save_msg_push(cls, **params):
@@ -265,6 +266,8 @@ class DevicePushService:
                             equipment_info_list.append(equipment_info_model(**equipment_info_data))
                         equipment_info_model.objects.bulk_create(equipment_info_list)
 
+            redis_obj.close()
+
             return True
         except Exception as e:
             LOGGING.info('推送消息或存表异常: error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))