Bird
0
0

How can you combine @Cacheable with a condition to cache only results where the returned value is not null?

hard📝 Application Q9 of 15
Spring Boot - Caching
How can you combine @Cacheable with a condition to cache only results where the returned value is not null?
A@Cacheable(cacheNames = "data", condition = "#result == null")
B@Cacheable(cacheNames = "data", condition = "#result != null")
C@Cacheable(cacheNames = "data", unless = "#result != null")
D@Cacheable(cacheNames = "data", unless = "#result == null")
Step-by-Step Solution
Solution:
  1. Step 1: Understand unless and condition attributes

    condition controls if caching happens before method call; unless prevents caching after method call based on result.

  2. Step 2: Choose correct attribute to skip caching null results

    To skip caching when result is null, use unless = "#result == null". @Cacheable(cacheNames = "data", unless = "#result == null") matches this. Others are incorrect logic.

  3. Final Answer:

    @Cacheable(cacheNames = "data", unless = "#result == null") -> Option D
  4. Quick Check:

    Use unless to skip caching null results [OK]
Quick Trick: Use unless to skip caching null results [OK]
Common Mistakes:
  • Confusing condition and unless
  • Using condition with #result which is unavailable
  • Wrong logic in expressions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes