title: Clear Redis Cache on Spring Boot Application Startup
body: <p>On application (spring boot service) startup, need to clear the Redis cache.</p>
  
  <p>Redis is running in a different docker container with own volume mapping. Since it retains the old cache, the application picks the data from Redis cache instead of Database even after the application restarts</p>
  
  <ul>
  <li>Tried <code>@EventListener</code> for <code>ContextRefreshedEvent</code> and it is never getting called. </li>
  <li>Tried with <code>@PostConstruct</code> in ApplicationMain class, but it doesn't clear the cache.</li>
  <li><p>Tried using <code>@CacheEvict(allEntries = true)</code>, but still no luck</p>
  
  <p>@Component
  public class ApplicationStartUp {</p>
  
  <pre><code>@Autowired
  private CacheManager cacheManager;
  
  @EventListener()
  public void onApplicationEvent(ContextStartedEvent event) {
      cacheManager.getCacheNames()
                  .parallelStream()
                  .forEach(n -&gt; cacheManager.getCache(n).clear());
  }
  </code></pre>
  
  <p>}</p></li>
  </ul>