mirror of
https://github.com/blossom-editor/blossom
synced 2024-11-17 14:39:21 +08:00
fix: 天气缓存问题
This commit is contained in:
parent
c1800969c6
commit
748caeafc0
@ -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<UserEntity> users = userService.listAll();
|
||||
Set<String> 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();
|
||||
|
@ -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<String, WeatherRes> 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<String, String> 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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user