Search code examples
springcachingspring-4spring-cache

Spring Cache Eviction


I have two services

class CustomerService {
   public void removeCustomer(int customerId) {...}
}

class OrderService {
   @Cacheable("orders")
   public List<Order> getOrders() {...}
}

On removeCustomer() I want the orders cache to be evicted, since the result has changed (the orders of the removed customers are gone). How can I do this with Spring's caching?


Solution

  • You can use @CacheEvict annotation for the same. Please refer to the below link for details

    https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/CacheEvict.html

    You can find example at

    https://dzone.com/articles/spring-31-caching-and-0