Bird
0
0

Given this method:

medium📝 Debug Q14 of 15
Spring Boot - Caching
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?
AThe key expression should be <code>#orderId</code> instead of <code>#order.id</code>
BThe method parameter 'order' is not accessible in SpEL key expression
CThe key expression <code>#order.id</code> requires the 'order' object to be non-null and have a valid id
D@CacheEvict cannot use complex key expressions
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes