添加天气缓存

This commit is contained in:
孙小云 2026-01-30 16:16:02 +08:00
parent f228313312
commit 557cd153c6
1 changed files with 63 additions and 35 deletions

View File

@ -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<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 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<>();
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<String, String> headers = new HashMap<>();
headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode());
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<>();
Map<String, String> 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;
}
});
}
/**