Bird
0
0

Given this method in a Spring Boot service:

medium📝 component behavior Q4 of 15
Spring Boot - Caching
Given this method in a Spring Boot service:
@CachePut(value = "items", key = "#item.id")
public Item updateItem(Item item) {
    item.setName(item.getName().toUpperCase());
    return item;
}
What will be cached after calling updateItem with an item named "book"?
AAn item with name "book" cached under the item's id key.
BAn item with name "BOOK" cached under a default key.
CNo caching occurs because the method modifies the item.
DAn item with name "BOOK" cached under the item's id key.
Step-by-Step Solution
Solution:
  1. Step 1: Understand method behavior and @CachePut effect

    The method changes the item's name to uppercase and returns it. @CachePut caches the returned value under the key '#item.id'.
  2. Step 2: Determine cached value and key

    The cached item will have name "BOOK" (uppercase) and be stored under the item's id key, not a default key.
  3. Final Answer:

    An item with name "BOOK" cached under the item's id key. -> Option D
  4. Quick Check:

    @CachePut caches returned updated value with specified key [OK]
Quick Trick: @CachePut caches method return value after update [OK]
Common Mistakes:
  • Assuming original item is cached without changes
  • Thinking no cache happens if method modifies object
  • Believing default key is used without key attribute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes