添加天气缓存
This commit is contained in:
parent
f228313312
commit
557cd153c6
|
|
@ -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,15 +25,41 @@ public class WeatherDomainImpl implements IWeatherDomain {
|
|||
@Autowired
|
||||
private WeatherProperties weatherProperties;
|
||||
|
||||
private final Random random = new Random();
|
||||
private final Cache<String, Weather> weatherCache;
|
||||
|
||||
public WeatherDomainImpl() {
|
||||
this.weatherCache = Caffeine.newBuilder()
|
||||
.expireAfter(new Expiry<String, Weather>() {
|
||||
@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 cacheKey = lat + "," + lon;
|
||||
return weatherCache.get(cacheKey, key -> {
|
||||
String host = weatherProperties.getHost();
|
||||
String path = weatherProperties.getPath();
|
||||
String method = "POST";
|
||||
Map<String, String> 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<String, String> querys = new HashMap<>();
|
||||
Map<String, String> bodys = new HashMap<>();
|
||||
|
|
@ -41,17 +72,13 @@ public class WeatherDomainImpl implements IWeatherDomain {
|
|||
|
||||
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));
|
||||
}
|
||||
|
|
@ -60,6 +87,7 @@ public class WeatherDomainImpl implements IWeatherDomain {
|
|||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue