|
@@ -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})
|