gpt4 book ai didi

java - 在 spring boot 中重新加载/刷新缓存

转载 作者:行者123 更新时间:2023-11-30 12:05:14 24 4
gpt4 key购买 nike

我正在使用 Spring Boot,我正在使用 Ehcache 进行缓存。到目前为止一切正常。但是现在我必须重新加载/刷新,所以我该怎么做才能使我的应用程序不会有任何停机时间。

我在 Spring Ehcache 中尝试了很多方法,但都没有用,否则必须编写调度程序并重新加载数据。

@Override
@Cacheable(value="partTypeCache", key="#partKey")
public List<PartType> loadPartType(String partKey) throws CustomException {
return productIdentityDao.loadPartType();
}

最佳答案

显然,关于您的问题的所有评论都是正确的。您应该使用 CacheEvict。我在这里找到了解决方案:https://www.baeldung.com/spring-boot-evict-cache它看起来像这样:

您所要做的就是创建名为例如CacheService 和 create 方法将驱逐您拥有的所有缓存对象。然后你注释该方法 @Scheduled 并输入你的间隔率。

@Service
public class CacheService {

@Autowired
CacheManager cacheManager;

public void evictAllCaches() {
cacheManager.getCacheNames().stream()
.forEach(cacheName -> cacheManager.getCache(cacheName).clear());
}

@Scheduled(fixedRate = 6000)
public void evictAllcachesAtIntervals() {
evictAllCaches();
}

}

关于java - 在 spring boot 中重新加载/刷新缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56458748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com