小编Sen*_*hil的帖子

Spring Boot 应用程序启动时清除 Redis 缓存

应用程序(spring boot服务)启动时,需要清除Redis缓存。

Redis 运行在具有自己的卷映射的不同 Docker 容器中。由于它保留了旧的缓存,因此即使在应用程序重新启动后,应用程序也会从 Redis 缓存而不是数据库中选取数据

  • 尝试@EventListenerContextRefreshedEvent但从未被调用过。
  • 尝试在 ApplicationMain 类中使用@PostConstruct,但它不会清除缓存。
  • 尝试使用@CacheEvict(allEntries = true),但仍然没有运气

    @Component 公共类ApplicationStartUp {

    @Autowired
    private CacheManager cacheManager;
    
    @EventListener()
    public void onApplicationEvent(ContextStartedEvent event) {
        cacheManager.getCacheNames()
                    .parallelStream()
                    .forEach(n -> cacheManager.getCache(n).clear());
    }
    
    Run Code Online (Sandbox Code Playgroud)

    }

spring caching redis spring-boot

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

caching ×1

redis ×1

spring ×1

spring-boot ×1