添加天气缓存

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,15 +25,41 @@ 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 cacheKey = lat + "," + lon;
return weatherCache.get(cacheKey, key -> {
String host = weatherProperties.getHost(); String host = weatherProperties.getHost();
String path = weatherProperties.getPath(); String path = weatherProperties.getPath();
String method = "POST"; String method = "POST";
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode()); headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode());
//根据API的要求定义相对应的Content-Type
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<>();
@ -41,17 +72,13 @@ public class WeatherDomainImpl implements IWeatherDomain {
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) { if (weatherResponse != null && weatherResponse.getCode() == 0) {
//转换风速将风力等级转换为风速中间值
String windLevel = weatherResponse.getData().getCondition().getWindLevel(); String windLevel = weatherResponse.getData().getCondition().getWindLevel();
weather.setWindSpeed(convertWindLevelToSpeed(windLevel)); weather.setWindSpeed(convertWindLevelToSpeed(windLevel));
weather.setEnvironmentTemperature(Double.valueOf(weatherResponse.getData().getCondition().getTemp())); weather.setEnvironmentTemperature(Double.valueOf(weatherResponse.getData().getCondition().getTemp()));
weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity())); weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity()));
//转换降雨量
String conditionId = weatherResponse.getData().getCondition().getConditionId(); String conditionId = weatherResponse.getData().getCondition().getConditionId();
weather.setRainfall(convertConditionIdToRainfall(conditionId)); weather.setRainfall(convertConditionIdToRainfall(conditionId));
} }
@ -60,6 +87,7 @@ public class WeatherDomainImpl implements IWeatherDomain {
} catch (Exception ignored) { } catch (Exception ignored) {
return null; return null;
} }
});
} }
/** /**