Challenge - 5 Problems
Cache Eviction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when @CacheEvict is used with allEntries=true?
Consider a Spring Boot service method annotated with
@CacheEvict(value = "items", allEntries = true). What is the effect on the cache when this method is called?Spring Boot
public class ItemService { @CacheEvict(value = "items", allEntries = true) public void clearAllItemsCache() { // method logic } }
Attempts:
2 left
💡 Hint
Think about what 'allEntries=true' means for cache eviction.
✗ Incorrect
Setting allEntries=true tells Spring to remove all entries from the specified cache, not just the one related to the method parameters.
📝 Syntax
intermediate2:00remaining
Which @CacheEvict annotation syntax correctly evicts a cache entry by key?
You want to evict a cache entry with a specific key passed as a method parameter named 'id'. Which annotation syntax is correct?
Spring Boot
public class UserService { @CacheEvict(value = "users", key = "#id") public void removeUser(Long id) { // method logic } }
Attempts:
2 left
💡 Hint
Spring SpEL expressions use # to reference method parameters.
✗ Incorrect
The key attribute uses Spring Expression Language (SpEL). To refer to the method parameter 'id', use #id.
🔧 Debug
advanced2:00remaining
Why does @CacheEvict not remove the cache entry as expected?
Given the code below, the cache entry is not removed after calling
deleteProduct(5L). What is the most likely cause?Spring Boot
public class ProductService { @CacheEvict(value = "products", key = "#productId") public void deleteProduct(Long id) { // deletion logic } }
Attempts:
2 left
💡 Hint
Check if the key expression matches the method parameter name.
✗ Incorrect
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.
❓ state_output
advanced2:00remaining
What is the cache state after calling a method with @CacheEvict(allEntries=true) and before method execution?
Consider a method annotated with
@CacheEvict(value = "orders", allEntries = true, beforeInvocation = true). What is the state of the 'orders' cache when the method throws an exception during execution?Spring Boot
public class OrderService { @CacheEvict(value = "orders", allEntries = true, beforeInvocation = true) public void cancelAllOrders() { throw new RuntimeException("Failure"); } }
Attempts:
2 left
💡 Hint
Consider the effect of beforeInvocation=true on eviction timing.
✗ Incorrect
With beforeInvocation=true, cache eviction happens before the method runs. So even if the method throws an exception, the cache is already cleared.
🧠 Conceptual
expert2:00remaining
Which statement best describes the use of @CacheEvict in Spring Boot?
Select the most accurate description of how @CacheEvict works in Spring Boot caching.
Attempts:
2 left
💡 Hint
Think about eviction timing and scope options.
✗ Incorrect
@CacheEvict is designed to remove cache entries. It can remove specific keys or all entries and supports eviction before or after the method runs.