java - 在 Spring Boot 中重新加载/刷新缓存
问题描述
我正在使用 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 的类并创建方法,该方法将驱逐您拥有的所有缓存对象。然后你注释该方法@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();
}
}
推荐阅读
- terraform - Terrafrom:如何使用附加名称查询 AWS ACM 证书?
- sql - 我需要一些帮助来解决 SQL 查询,我需要列出要求至少一种产品但不要求牛奶的消费者的姓名
- html - 在没有滑块的面板上显示长文本
- python - 为什么我的 Flask 服务器在部署到 Docker 容器后无法连接?
- c# - 尝试更改 MainPage 但新页面上的 WebView 无法加载站点。但是当我在应用程序启动时将它设置为主页时它是成功的
- sql - 多连接选择
- android - Jetpack 的 BiometricPrompt (androidx.biometric.BiometricPrompt) 抛出 NullPointerException
- java - 如何回滚捕获异常的事务?
- html - 响应式轮播库
- javascript - 访问其他HTML页面时如何避免与当前socket.io连接断开