Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand cache behavior

    Cache stores data to speed up repeated access, but stale data can cause issues.
  2. Step 2: Role of @CacheEvict

    @CacheEvict is used to remove outdated cache entries after data changes to keep cache fresh.
  3. Final Answer:

    To remove or invalidate cache entries after data changes -> Option A
  4. 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

  1. Step 1: Identify correct attribute names

    The correct attribute to specify cache names is cacheNames or value. To clear all entries, use allEntries = true.
  2. Step 2: Match correct syntax

    @CacheEvict(cacheNames = "users", allEntries = true) uses @CacheEvict(cacheNames = "users", allEntries = true), which is valid syntax.
  3. Final Answer:

    @CacheEvict(cacheNames = "users", allEntries = true) -> Option A
  4. 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
  • Confusing allEntries with clearAll or removeAll
  • Missing quotes around cache name
3. Consider this method in a Spring Boot service:
@CacheEvict(cacheNames = "products", key = "#id")
public void deleteProduct(Long id) {
    productRepository.deleteById(id);
}
What happens when deleteProduct(5L) is called?
medium
A. All entries in 'products' cache are removed
B. The cache entry with key 5 in 'products' cache is removed
C. A new cache entry with key 5 is added
D. The method throws a runtime error due to missing key

Solution

  1. Step 1: Analyze @CacheEvict usage

    The annotation specifies cacheNames = "products" and key = "#id", so it targets the cache entry with the given id.
  2. Step 2: Effect of calling deleteProduct(5L)

    Calling with id 5 removes the cache entry with key 5 from the 'products' cache.
  3. Final Answer:

    The cache entry with key 5 in 'products' cache is removed -> Option B
  4. Quick Check:

    @CacheEvict with key removes specific cache entry [OK]
Hint: Key in @CacheEvict targets specific cache entry [OK]
Common Mistakes:
  • Assuming all cache entries are removed without allEntries=true
  • Thinking it adds cache entries instead of removing
  • Believing missing key causes runtime error
4. Given this method:
@CacheEvict(cacheNames = "orders", key = "#order.id")
public void cancelOrder(Order order) {
    orderRepository.delete(order);
}
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

  1. 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.
  2. 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.
  3. Final Answer:

    The key expression #order.id requires the 'order' object to be non-null and have a valid id -> Option C
  4. 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

  1. Step 1: Understand requirement

    We want to clear all entries in "sessions" cache only, not affect "users" cache.
  2. 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.
  3. Final Answer:

    @CacheEvict(cacheNames = "sessions", allEntries = true) on deleteUser method -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard