From 748caeafc00f7a24aa7e466213510547ad547037 Mon Sep 17 00:00:00 2001 From: xiaozzzi <42293085+xiaozzzi@users.noreply.github.com> Date: Fri, 2 Feb 2024 17:58:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=A9=E6=B0=94=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../thirdparty/ThirdPartyScheduled.java | 6 +--- .../thirdparty/hefeng/WeatherManager.java | 33 ++++++++++++++----- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/ThirdPartyScheduled.java b/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/ThirdPartyScheduled.java index 404f019..72b5f09 100644 --- a/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/ThirdPartyScheduled.java +++ b/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/ThirdPartyScheduled.java @@ -4,8 +4,6 @@ import com.blossom.backend.base.user.UserService; import com.blossom.backend.base.user.pojo.UserEntity; import com.blossom.backend.thirdparty.hefeng.WeatherManager; import com.blossom.common.base.pojo.R; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -26,7 +24,6 @@ import java.util.stream.Collectors; @RestController @RequestMapping("/thirdparty/scheduled") public class ThirdPartyScheduled { - private static final Logger log = LoggerFactory.getLogger(ThirdPartyScheduled.class); @Autowired private WeatherManager weatherManager; @@ -42,11 +39,10 @@ public class ThirdPartyScheduled { @PostMapping("/weather") @Scheduled(cron = "0 0/30 * * * ?") public R refreshWeather() { - log.debug("[BLOSSOM] 刷新天气"); List users = userService.listAll(); Set locations = users.stream().collect(Collectors.groupingBy(UserEntity::getLocation)).keySet(); for (String location : locations) { - weatherManager.clearAll(location); + weatherManager.clear(location); weatherManager.findWeatherAll(location); } return R.ok(); diff --git a/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/hefeng/WeatherManager.java b/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/hefeng/WeatherManager.java index 3b84db0..f1a618e 100644 --- a/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/hefeng/WeatherManager.java +++ b/blossom-backend/backend/src/main/java/com/blossom/backend/thirdparty/hefeng/WeatherManager.java @@ -9,17 +9,19 @@ import com.blossom.backend.thirdparty.hefeng.pojo.*; import com.blossom.common.base.exception.XzException400; import com.blossom.common.base.util.json.JsonUtil; import com.blossom.common.base.util.okhttp.HttpUtil; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.RemovalCause; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.CacheEvict; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * 天气查询 @@ -38,19 +40,32 @@ public class WeatherManager { private static final String URL_NOW = "https://devapi.heweather.net/v7/weather/now"; private static final String URL_DAILY = "https://devapi.heweather.net/v7/weather/3d"; private static final String URL_HOURLY = "https://devapi.heweather.net/v7/weather/24h"; - - private static final String WEATHER_ALL = "weather_all"; - private static final String SUCCESS = "200"; + /** + * + */ + private final Cache weatherCache = Caffeine.newBuilder() + .initialCapacity(50) + .expireAfterWrite(45, TimeUnit.MINUTES) + .removalListener((String location, WeatherRes weather, RemovalCause cause) -> + log.info("Weather cache [" + location + "] has been deleted") + ) + .build(); + @Autowired private ParamService paramService; /** * 查询天气信息 */ - @Cacheable(cacheNames = WEATHER_ALL, key = "'location_' + #location", unless = "#result == null") public WeatherRes findWeatherAll(String location) { + WeatherRes cache = weatherCache.getIfPresent(location); + if (cache != null) { + log.debug("[BLOSSOM] get weather from cache: {}", location); + return cache; + } + log.info("[BLOSSOM] refresh weather: {}", location); Map maps = initParam(location); if (maps == null) { log.info("未配置天气信息, 忽略天气查询"); @@ -128,15 +143,15 @@ public class WeatherManager { } else { log.error("获取小时预报失败, resp: {}", cityStr); } + weatherCache.put(location, weather); return weather; } /** * 清除缓存 */ - @CacheEvict(cacheNames = WEATHER_ALL, key = "'location_' + #location") - public void clearAll(String location) { - + public void clear(String location) { + weatherCache.invalidate(location); } /**