0
0
Spring Bootframework~20 mins

@CacheEvict for invalidation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Eviction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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
  }
}
AA new cache entry is added instead of removing any entries.
BOnly the cache entry corresponding to the method's parameters is removed.
CThe cache is not affected because allEntries=true disables eviction.
DAll entries in the 'items' cache are removed when the method is called.
Attempts:
2 left
💡 Hint
Think about what 'allEntries=true' means for cache eviction.
📝 Syntax
intermediate
2: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
  }
}
A"id"
B"#id"
C"$id"
D"{id}"
Attempts:
2 left
💡 Hint
Spring SpEL expressions use # to reference method parameters.
🔧 Debug
advanced
2: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
  }
}
AThe key expression uses 'productId' but the method parameter is named 'id'.
BThe cache name 'products' is misspelled.
CThe method must return a value to trigger cache eviction.
DThe @CacheEvict annotation requires allEntries=true to work.
Attempts:
2 left
💡 Hint
Check if the key expression matches the method parameter name.
state_output
advanced
2: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");
  }
}
AThe 'orders' cache is cleared before the exception is thrown.
BThe 'orders' cache remains unchanged because the method failed.
COnly some entries are removed depending on the exception.
DThe cache eviction happens after the exception, so no entries are removed.
Attempts:
2 left
💡 Hint
Consider the effect of beforeInvocation=true on eviction timing.
🧠 Conceptual
expert
2: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.
A@CacheEvict only works with methods that return a value and cannot clear all cache entries.
B@CacheEvict automatically updates cache entries with new values after method execution.
C@CacheEvict removes cache entries either by key or all entries, and can be configured to run before or after method execution.
D@CacheEvict disables caching for the annotated method but does not remove existing cache entries.
Attempts:
2 left
💡 Hint
Think about eviction timing and scope options.