Parcourir la source

优化设备获取地区天气接口

peng il y a 1 an
Parent
commit
e076f5ebe8
3 fichiers modifiés avec 12 ajouts et 12 suppressions
  1. 4 4
      Controller/WeatherControl.py
  2. 0 7
      Object/IPWeatherObject.py
  3. 8 1
      Object/RedisObject.py

+ 4 - 4
Controller/WeatherControl.py

@@ -60,11 +60,10 @@ class WeatherView(View):
         time_stamp = CommonService.str_to_timestamp(now_time.strftime('%Y-%m-%d %H:%M:%S'))
         temp, humidity = None, None
         try:
-            weather = redis_obj.get_data('city_id_{}_{}_weather'.format(city_id, time_stamp))
+            weather = redis_obj.get_hash_data('city_id_{}_weather'.format(city_id), time_stamp)
             if weather:
                 temp, humidity = weather.split('/')
             else:
-                LOGGER.info('redis不存在天气:{}'.format('city_id_{}_{}_weather'.format(city_id, time_stamp)))
                 city_obj = WeatherInfo(city_id)
                 weather_list = city_obj.get_city_24_weather()
                 if weather_list:
@@ -72,8 +71,9 @@ class WeatherView(View):
                     humidity = weather_list[0]['humidity']
                     for item in weather_list:
                         temp_time = CommonService.str_to_timestamp(item['date'] + ' ' + item['hour'] + ':00:00')
-                        redis_obj.set_ex_data('city_id_{}_{}_weather'.format(city_id, temp_time),
-                                              '{}/{}'.format(item['temp'], item['humidity']), 86400)
+                        redis_obj.set_hash_data('city_id_{}_weather'.format(city_id),
+                                                {temp_time: '{}/{}'.format(item['temp'], item['humidity'])})
+                    redis_obj.set_expire('city_id_{}_weather'.format(city_id), 86400)
             if not all([temp, humidity]):
                 return response.json(10, '获取天气失败')
             return response.json(0, {'temp': temp, 'humidity': humidity})

+ 0 - 7
Object/IPWeatherObject.py

@@ -4,7 +4,6 @@
 import requests
 
 from Model.models import IPAddr
-from Object.RedisObject import RedisObject
 
 
 class IPQuery:
@@ -76,12 +75,6 @@ class WeatherInfo:
         url = 'https://aliv18.data.moji.com/whapi/json/alicityweather/forecast24hours'  # 获取城市24小时天气
         data = {'cityId': self.city_id}
         response = requests.post(url, headers=self.headers, data=data, verify=False)
-        redis_obj = RedisObject()
-        count = redis_obj.get_data('weather_count')
-        if count:
-            redis_obj.set_data('weather_count', int(count) + 1)
-        else:
-            redis_obj.set_data('weather_count', 1)
         if response.status_code == 200:
             result = response.json()
             if result['code'] == 0:

+ 8 - 1
Object/RedisObject.py

@@ -20,6 +20,9 @@ class RedisObject:
         else:
             return True
 
+    def set_expire(self, key, ttl):
+        self.CONN.expire(key, ttl)
+
     def get_data(self, key):
         try:
             val = self.CONN.get(key)
@@ -97,7 +100,11 @@ class RedisObject:
         self.CONN.hmset(key, kwargs)
 
     def get_hash_data(self, key, file):
-        return self.CONN.hmget(key, file)
+        value = self.CONN.hmget(key, file)[0]
+        if value:
+            return value.decode('utf-8')
+        else:
+            return False
 
     def get_all_hash_data(self, key):
         return self.CONN.hgetall(key)