Complete the code to evict the cache named 'books' after the method runs.
@CacheEvict(cacheNames = "[1]") public void clearCache() { // method body }
The @CacheEvict annotation uses the cacheNames attribute to specify which cache to clear. Here, 'books' is the correct cache name to evict.
Complete the code to evict all entries in the 'books' cache after the method runs.
@CacheEvict(cacheNames = "books", [1] = true) public void clearAllBooksCache() { // method body }
The allEntries attribute set to true tells Spring to evict all entries in the specified cache.
Fix the error in the code to evict the cache entry with key '123' from the 'books' cache.
@CacheEvict(cacheNames = "books", key = "[1]") public void evictBook() { // method body }
The key attribute requires a SpEL expression as a string. The key '123' must be enclosed in single quotes inside the double quotes.
Fill both blanks to evict the cache entry with key 'id' before the method runs.
@CacheEvict(cacheNames = "books", key = "[1]", [2] = true) public void evictBefore(Long id) { // method body }
The key should be #id to refer to the method parameter. The beforeInvocation attribute set to true evicts the cache before the method runs.
Fill all three blanks to evict the cache entry with key 'user.id' only if the user's status is 'inactive'.
@CacheEvict(cacheNames = "users", key = "[1]", condition = "[2] == 'inactive'", [3] = false) public void evictInactiveUser(User user) { // method body }
The key uses #user.id to access the user's id. The condition checks if #user.status equals 'inactive'. The beforeInvocation attribute set to false means eviction happens after method runs.