Browse Source

修改天气

yq 2 years ago
parent
commit
abea79c118

+ 12 - 13
src/main/java/com/usky/dxtop/controller/web/business/WeatherControllerWeb.java

@@ -1,19 +1,20 @@
 package com.usky.dxtop.controller.web.business;
 
 
+import com.usky.dxtop.common.core.redis.RedisCache;
 import com.usky.dxtop.common.exception.CustomException;
-import com.usky.dxtop.common.utils.StringUtils;
 import com.usky.dxtop.common.utils.http.HttpUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.io.IOException;
 import java.net.URLEncoder;
-import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author yq
@@ -29,22 +30,20 @@ public class WeatherControllerWeb {
     private static final String ALI_WEATHER_APPCODE = "0f2b7fce6e104ba8835358b7b59b4fb6";
     private static final String ALI_WEATHER_HEADER_VALUE = "APPCODE " + ALI_WEATHER_APPCODE;
 
-    private String weather = "";
 
-    private Date date = null;
+    @Autowired
+    private RedisCache redisCache;
 
     @GetMapping()
     public String get(@RequestParam String area) {
-        if (StringUtils.isBlank(weather)){
-            weather = getWeatherApi(area);
-            date = new Date();
+        Object cacheObject = redisCache.getCacheObject(area);
+        if (Objects.nonNull(cacheObject)){
+            return cacheObject.toString();
         }else {
-            if ((System.currentTimeMillis() - date.getTime()) >= (1000 * 60 * 60 * 5)){
-                weather = getWeatherApi(area);
-                date = new Date();
-            }
+            String weatherApi = getWeatherApi(area);
+            redisCache.setCacheObject(area,weatherApi,30, TimeUnit.MINUTES);
+            return weatherApi;
         }
-        return weather;
     }
     public String getWeatherApi(String area){
         try {