添加天气缓存

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