Explorar o código

获取设备地区天气限流

peng %!s(int64=2) %!d(string=hai) anos
pai
achega
b68c827dfd
Modificáronse 2 ficheiros con 12 adicións e 3 borrados
  1. 11 2
      Controller/WeatherControl.py
  2. 1 1
      Object/ApschedulerObject.py

+ 11 - 2
Controller/WeatherControl.py

@@ -11,6 +11,7 @@ from django.views import View
 from Object.IPWeatherObject import WeatherInfo
 from Model.models import UidSetModel, CityInformation
 from Object.ResponseObject import ResponseObject
+from Object.RedisObject import RedisObject
 
 
 class WeatherView(View):
@@ -47,9 +48,17 @@ class WeatherView(View):
         uid_set_qs = UidSetModel.objects.filter(uid=uid).values('tb_city_information_id')
         if not uid_set_qs.exists():
             return response.json(173)
+        redis_obj = RedisObject()
+        city_id = uid_set_qs[0]['tb_city_information_id']
         try:
-            city_obj = WeatherInfo(uid_set_qs[0]['tb_city_information_id'])
-            temp, humidity = city_obj.get_city_weather()
+            temp = redis_obj.get_data('city_id_{}_temp'.format(city_id))
+            humidity = redis_obj.get_data('city_id_{}_humidity'.format(city_id))
+            if not all([temp, humidity]):
+                city_obj = WeatherInfo(city_id)
+                temp, humidity = city_obj.get_city_weather()
+                if temp and humidity:
+                    redis_obj.set_ex_data('city_id_{}_temp'.format(city_id), temp, 600)
+                    redis_obj.set_ex_data('city_id_{}_humidity'.format(city_id), humidity, 600)
             if not all([temp, humidity]):
                 return response.json(10, '获取天气失败')
             return response.json(0, {'temp': temp, 'humidity': humidity})

+ 1 - 1
Object/ApschedulerObject.py

@@ -17,7 +17,7 @@ class ApschedulerObject:
         now_time = time.time()
         print('hello world:[{}]'.format(now_time))
 
-    def cron_job(self, task_id, day_of_week, hour, minute):  # 定时任务
+    def cron_job(self, task_id, day_of_week, hour, minute):  # 周期任务
         self.scheduler.add_job(self.auto_hello, 'cron', day_of_week=day_of_week, hour=hour, minute=minute,
                                replace_existing=True, id=task_id, max_instances=1, coalesce=True)