Bird
0
0

Given the following Spring Boot method with Redis caching:

medium📝 component behavior Q13 of 15
Spring Boot - Caching
Given the following Spring Boot method with Redis caching:
@Cacheable("books")
public Book findBookById(Long id) {
    System.out.println("Fetching from DB");
    return bookRepository.findById(id).orElse(null);
}
What will be printed if findBookById(1L) is called twice in a row?
AFetching from DB
BNo output
CFetching from DB Fetching from DB
DError at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Understand @Cacheable behavior

    The first call caches the result and prints "Fetching from DB".
  2. Step 2: Analyze second call output

    The second call returns cached data without executing method body, so no print.
  3. Final Answer:

    Fetching from DB -> Option A
  4. Quick Check:

    Cache avoids repeated DB fetch print [OK]
Quick Trick: First call prints, second uses cache silently [OK]
Common Mistakes:
  • Expecting print on every call
  • Thinking cache disables method
  • Assuming runtime error due to caching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes