diff --git a/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java b/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java index 5871927..f1f7ba5 100644 --- a/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java +++ b/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java @@ -1,6 +1,9 @@ package com.ruoyi.device.domain.impl; import com.alibaba.fastjson2.JSON; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.Expiry; import com.ruoyi.device.domain.api.IWeatherDomain; import com.ruoyi.device.domain.impl.weather.HttpUtils; import com.ruoyi.device.domain.model.weather.Weather; @@ -13,6 +16,8 @@ import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; +import java.util.Random; +import java.util.concurrent.TimeUnit; @Component public class WeatherDomainImpl implements IWeatherDomain { @@ -20,46 +25,69 @@ public class WeatherDomainImpl implements IWeatherDomain { @Autowired private WeatherProperties weatherProperties; + private final Random random = new Random(); + private final Cache weatherCache; + + public WeatherDomainImpl() { + this.weatherCache = Caffeine.newBuilder() + .expireAfter(new Expiry() { + @Override + public long expireAfterCreate(String key, Weather value, long currentTime) { + long randomMinutes = 1 + random.nextInt(10); + return TimeUnit.MINUTES.toNanos(randomMinutes); + } + + @Override + public long expireAfterUpdate(String key, Weather value, long currentTime, long currentDuration) { + long randomMinutes = 1 + random.nextInt(10); + return TimeUnit.MINUTES.toNanos(randomMinutes); + } + + @Override + public long expireAfterRead(String key, Weather value, long currentTime, long currentDuration) { + return currentDuration; + } + }) + .build(); + } + public Weather weatherInfo(String lat, String lon) { - String host = weatherProperties.getHost(); - String path = weatherProperties.getPath(); - String method = "POST"; - Map headers = new HashMap<>(); - //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105 - headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode()); - //根据API的要求,定义相对应的Content-Type - headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - Map querys = new HashMap<>(); - Map bodys = new HashMap<>(); - bodys.put("lat", lat); - bodys.put("lon", lon); - bodys.put("token", weatherProperties.getToken()); + String cacheKey = lat + "," + lon; + return weatherCache.get(cacheKey, key -> { + String host = weatherProperties.getHost(); + String path = weatherProperties.getPath(); + String method = "POST"; + Map headers = new HashMap<>(); + headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode()); + headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + Map querys = new HashMap<>(); + Map bodys = new HashMap<>(); + bodys.put("lat", lat); + bodys.put("lon", lon); + bodys.put("token", weatherProperties.getToken()); - Weather weather = new Weather(); - try { + Weather weather = new Weather(); + try { - HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); - String json = EntityUtils.toString(response.getEntity()); - // 将JSON字符串转换为对象 - WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class); - // 降雨量(枚举值:0-无雨,1-小雨,2-中雨,3-大雨) - // 使用转换后的对象 - if (weatherResponse != null && weatherResponse.getCode() == 0) { - //转换风速:将风力等级转换为风速中间值 - String windLevel = weatherResponse.getData().getCondition().getWindLevel(); - weather.setWindSpeed(convertWindLevelToSpeed(windLevel)); - weather.setEnvironmentTemperature(Double.valueOf(weatherResponse.getData().getCondition().getTemp())); - weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity())); - //转换降雨量 - String conditionId = weatherResponse.getData().getCondition().getConditionId(); - weather.setRainfall(convertConditionIdToRainfall(conditionId)); + HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); + String json = EntityUtils.toString(response.getEntity()); + WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class); + + if (weatherResponse != null && weatherResponse.getCode() == 0) { + String windLevel = weatherResponse.getData().getCondition().getWindLevel(); + weather.setWindSpeed(convertWindLevelToSpeed(windLevel)); + weather.setEnvironmentTemperature(Double.valueOf(weatherResponse.getData().getCondition().getTemp())); + weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity())); + String conditionId = weatherResponse.getData().getCondition().getConditionId(); + weather.setRainfall(convertConditionIdToRainfall(conditionId)); + } + + return weather; + } catch (Exception ignored) { + return null; } - - return weather; - } catch (Exception ignored) { - return null; - } + }); } /**