我正在使用 Spring Boot,对于缓存,我使用 Ehcache。到目前为止一切正常。但现在我必须重新加载/刷新,那么我该如何执行此操作,以便我的应用程序不会出现任何停机时间。
我在Spring Ehcache中尝试了很多方法,但它不起作用,否则必须编写调度程序并重新加载数据。
@Override
@Cacheable(value="partTypeCache", key="#partKey")
public List<PartType> loadPartType(String partKey) throws CustomException {
return productIdentityDao.loadPartType();
}
显然关于你的问题的所有评论都是正确的。你should使用CacheEvict。我在这里找到了解决方案:https://www.baeldung.com/spring-boot-evict-cache 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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)