在应用程序(Spring Boot服务)启动时,需要清除Redis缓存。
Redis运行在不同的Docker容器中,并具有自己的卷映射。因为它保留了旧的缓存,所以即使应用程序重新启动,它也会从Redis缓存中获取数据而不是数据库。
- Tried
@EventListener
forContextRefreshedEvent
and it is never getting called. - Tried with
@PostConstruct
in ApplicationMain class, but it doesn't clear the cache. Tried using
@CacheEvict(allEntries = true)
, but still no luck@Component public class ApplicationStartUp {
@Autowired private CacheManager cacheManager; @EventListener() public void onApplicationEvent(ContextStartedEvent event) { cacheManager.getCacheNames() .parallelStream() .forEach(n -> cacheManager.getCache(n).clear()); }
}
cacheManager.getCacheNames()
的结果,您能看到缓存名称吗?在启动时,缓存管理器可能不会返回任何内容。作为测试,请尝试使用@PostConstruct
和cacheManger.getCache("your cache").clear()
。 - chrsblck