Which combination of annotations and configuration correctly achieves this in Spring Boot?
hard📝 Application Q15 of 15
Spring Boot - Caching
You want to cache results of a method that takes a user ID and returns user details. You also want to clear the cache when user data updates. Which combination of annotations and configuration correctly achieves this in Spring Boot?
AUse <code>@EnableCaching</code> on service class, <code>@CachePut("users")</code> on get method, and no annotation on update method
BUse <code>@CacheEvict</code> on config class, <code>@EnableCaching</code> on update method, and no caching on get method
CUse <code>@Cacheable("users")</code> on config class, <code>@EnableCaching</code> on get method, and <code>@CacheEvict</code> on update method
DUse <code>@EnableCaching</code> on config class, <code>@Cacheable("users")</code> on get method, and <code>@CacheEvict(value = "users", key = "#userId")</code> on update method
Step-by-Step Solution
Solution:
Step 1: Enable caching globally
Add @EnableCaching on a configuration or main class to activate caching support.
Step 2: Cache method results and evict on update
Use @Cacheable("users") on the method fetching user details to cache results. Use @CacheEvict(value = "users", key = "#userId") on the update method to clear cache for that user.
Final Answer:
Use @EnableCaching on config class, @Cacheable("users") on get method, and @CacheEvict(value = "users", key = "#userId") on update method -> Option D
Quick Check:
Enable caching + cache get + evict on update [OK]
Quick Trick:Enable caching, cache gets, evict on updates [OK]
Common Mistakes:
Not enabling caching globally with @EnableCaching
Using @CachePut instead of @CacheEvict for clearing cache
Placing annotations on wrong classes or methods
Master "Caching" in Spring Boot
9 interactive learning modes - each teaches the same concept differently