What is the purpose of the @CacheEvict annotation in Spring Boot?
The @CacheEvict annotation is used to remove or invalidate cache entries when a method is called, ensuring that stale data is not served from the cache.
Click to reveal answer
beginner
How do you specify which cache to evict using @CacheEvict?
You specify the cache name(s) to evict using the 'value' or 'cacheNames' attribute, for example: @CacheEvict(value = "usersCache").
Click to reveal answer
intermediate
What does the 'allEntries' attribute do in @CacheEvict?
Setting 'allEntries = true' removes all entries from the specified cache instead of just the one related to the method's key.
Click to reveal answer
intermediate
Explain the use of 'beforeInvocation' attribute in @CacheEvict.
The 'beforeInvocation' attribute controls whether the cache eviction happens before the method runs (true) or after it completes (false). By default, eviction happens after the method runs.
Click to reveal answer
intermediate
Can @CacheEvict be used to evict multiple caches at once? How?
Yes, by specifying multiple cache names in the 'value' or 'cacheNames' attribute as an array, e.g., @CacheEvict(value = {"cache1", "cache2"}).
Click to reveal answer
What happens if you set allEntries = true in @CacheEvict?
AAll entries in the specified cache are removed
BThe cache entries are refreshed automatically
CThe cache is disabled permanently
DOnly the cache entry related to the method key is removed
✗ Incorrect
Setting allEntries = true removes all entries from the cache, not just the one related to the method key.
When does cache eviction occur by default with @CacheEvict?
ABefore the annotated method runs
BOnly if the method throws an exception
CAfter the annotated method completes successfully
DImmediately when the application starts
✗ Incorrect
By default, cache eviction happens after the annotated method completes successfully.
How do you specify multiple caches to evict in @CacheEvict?
AYou cannot evict multiple caches at once
BUse multiple @CacheEvict annotations
CUse the cacheNames attribute with a single string
DPass an array of cache names to the value attribute
✗ Incorrect
You can specify multiple caches by passing an array of cache names to the value or cacheNames attribute.
What does setting beforeInvocation = true do in @CacheEvict?
AEvicts cache entries before the method runs
BEvicts cache entries after the method runs
CPrevents cache eviction
DEvicts cache only if the method throws an exception
✗ Incorrect
Setting beforeInvocation = true causes cache eviction to happen before the annotated method runs.
Which attribute in @CacheEvict defines the cache to clear?
Akey
Bvalue or cacheNames
Ccondition
Dsync
✗ Incorrect
The value or cacheNames attribute specifies which cache(s) to evict entries from.
Describe how @CacheEvict helps keep cache data fresh in a Spring Boot application.
Think about when and why you want to clear cache entries.
You got /4 concepts.
Explain the difference between evicting cache before vs after method execution using @CacheEvict.
Consider what happens if the method fails or throws an error.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the @CacheEvict annotation in Spring Boot?
easy
A. To remove or invalidate cache entries after data changes
B. To add new entries to the cache
C. To configure cache storage location
D. To read data from the cache without modification
Solution
Step 1: Understand cache behavior
Cache stores data to speed up repeated access, but stale data can cause issues.
Step 2: Role of @CacheEvict
@CacheEvict is used to remove outdated cache entries after data changes to keep cache fresh.
Final Answer:
To remove or invalidate cache entries after data changes -> Option A
Quick Check:
@CacheEvict removes cache entries [OK]
Hint: Remember: @CacheEvict clears cache after updates [OK]
Common Mistakes:
Confusing @CacheEvict with @CachePut or @Cacheable
Thinking it adds cache entries instead of removing
Assuming it configures cache storage
2. Which of the following is the correct way to use @CacheEvict to clear a cache named "users" after a method executes?
easy
A. @CacheEvict(cacheNames = "users", allEntries = true)
B. @CacheEvict(cacheName = "users", clearAll = true)
C. @CacheEvict(value = "users", removeAll = true)
D. @CacheEvict(cache = "users", evictAll = true)
Solution
Step 1: Identify correct attribute names
The correct attribute to specify cache names is cacheNames or value. To clear all entries, use allEntries = true.
Step 2: Match correct syntax
@CacheEvict(cacheNames = "users", allEntries = true) uses @CacheEvict(cacheNames = "users", allEntries = true), which is valid syntax.
Final Answer:
@CacheEvict(cacheNames = "users", allEntries = true) -> Option A
Quick Check:
cacheNames + allEntries = true [OK]
Hint: Use cacheNames and allEntries=true to clear all cache keys [OK]
Common Mistakes:
Using wrong attribute names like cacheName or cache
Why might the cache eviction not work as expected?
medium
A. The key expression should be #orderId instead of #order.id
B. The method parameter 'order' is not accessible in SpEL key expression
C. The key expression #order.id requires the 'order' object to be non-null and have a valid id
D. @CacheEvict cannot use complex key expressions
Solution
Step 1: Understand SpEL key expression
The key expression #order.id accesses the id property of the 'order' parameter. This requires 'order' to be non-null and have a valid id.
Step 2: Identify why eviction might fail
If 'order' is null or its id is null, the cache key cannot be resolved, so eviction won't work as expected.
Final Answer:
The key expression #order.id requires the 'order' object to be non-null and have a valid id -> Option C
Quick Check:
SpEL key needs valid object and property [OK]
Hint: Ensure key objects are non-null with valid properties [OK]
Common Mistakes:
Assuming key expressions cannot use object properties
Using wrong parameter names in key expression
Believing @CacheEvict cannot handle complex keys
5. You have a Spring Boot service with two caches: "users" and "sessions". You want to clear all entries in "sessions" cache whenever a user is deleted, but keep "users" cache intact. Which @CacheEvict usage correctly achieves this in the deleteUser method?
public void deleteUser(Long userId) {
userRepository.deleteById(userId);
// cache eviction here
}
hard
A. @CacheEvict(cacheNames = "sessions", key = "#userId") on deleteUser method
B. @CacheEvict(cacheNames = {"users", "sessions"}, allEntries = true) on deleteUser method
C. @CacheEvict(cacheNames = "users", key = "#userId") on deleteUser method
D. @CacheEvict(cacheNames = "sessions", allEntries = true) on deleteUser method
Solution
Step 1: Understand requirement
We want to clear all entries in "sessions" cache only, not affect "users" cache.
Step 2: Analyze options
@CacheEvict(cacheNames = "sessions", allEntries = true) on deleteUser method clears all entries in "sessions" cache, matching requirement. @CacheEvict(cacheNames = {"users", "sessions"}, allEntries = true) on deleteUser method clears both caches, which is incorrect. @CacheEvict(cacheNames = "users", key = "#userId") on deleteUser method clears only one user key in "users" cache, not sessions. @CacheEvict(cacheNames = "sessions", key = "#userId") on deleteUser method clears one key in "sessions" cache, not all.
Final Answer:
@CacheEvict(cacheNames = "sessions", allEntries = true) on deleteUser method -> Option D
Quick Check:
allEntries=true clears entire cache [OK]
Hint: Use allEntries=true on target cache to clear all keys [OK]
Common Mistakes:
Clearing wrong cache or multiple caches unintentionally
Using key instead of allEntries to clear entire cache
Assuming deleting user clears user cache automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.