@CacheEvict(value = "items", allEntries = true). What is the effect on the cache when this method is called?public class ItemService { @CacheEvict(value = "items", allEntries = true) public void clearAllItemsCache() { // method logic } }
Setting allEntries=true tells Spring to remove all entries from the specified cache, not just the one related to the method parameters.
public class UserService { @CacheEvict(value = "users", key = "#id") public void removeUser(Long id) { // method logic } }
The key attribute uses Spring Expression Language (SpEL). To refer to the method parameter 'id', use #id.
deleteProduct(5L). What is the most likely cause?public class ProductService { @CacheEvict(value = "products", key = "#productId") public void deleteProduct(Long id) { // deletion logic } }
The key expression #productId does not match the method parameter id. This mismatch causes Spring to look for a non-existent parameter, so eviction fails.
@CacheEvict(value = "orders", allEntries = true, beforeInvocation = true). What is the state of the 'orders' cache when the method throws an exception during execution?public class OrderService { @CacheEvict(value = "orders", allEntries = true, beforeInvocation = true) public void cancelAllOrders() { throw new RuntimeException("Failure"); } }
With beforeInvocation=true, cache eviction happens before the method runs. So even if the method throws an exception, the cache is already cleared.
@CacheEvict is designed to remove cache entries. It can remove specific keys or all entries and supports eviction before or after the method runs.
