Bird
0
0

Consider this method in a Spring Boot service:

medium📝 component behavior Q13 of 15
Spring Boot - Caching
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?
AAll entries in 'products' cache are removed
BThe cache entry with key 5 in 'products' cache is removed
CA new cache entry with key 5 is added
DThe method throws a runtime error due to missing key
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes