Performance: @EnableCaching annotation
This annotation affects server-side response time by enabling caching, which reduces repeated data processing and speeds up response delivery.
// Add @EnableCaching to @SpringBootApplication or @Configuration class @Service public class ItemService { @Cacheable("items") public List<Item> getItems() { return itemRepository.findAll(); } }
public List<Item> getItems() {
// fetches from database every time
return itemRepository.findAll();
}| Pattern | Server Processing | Database Calls | Memory Usage | Verdict |
|---|---|---|---|---|
| No caching | High on repeated calls | One per request | Low | [X] Bad |
| With @EnableCaching | Low after first call | Single call cached | Increased due to cache | [OK] Good |
@EnableCaching annotation in a Spring Boot application?@EnableCaching role@Cacheable@Cacheable store their results in cache only if caching is enabled.@Cacheable can cache results -> Option A@EnableCaching enables caching [OK]@EnableCaching.@EnableCaching [OK]getData() is called twice?@EnableCaching
@Service
public class DataService {
private int count = 0;
@Cacheable("dataCache")
public String getData() {
count++;
return "Call number: " + count;
}
}getData() runs the method, increments count to 1, and caches the result.@Configuration
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("items");
}
}@EnableCaching.@Service is unrelated here.@EnableCaching annotation on configuration class -> Option B@EnableCaching [OK]@EnableCaching on a configuration or main class to activate caching support.@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.@EnableCaching on config class, @Cacheable("users") on get method, and @CacheEvict(value = "users", key = "#userId") on update method -> Option DPass the test and this pattern moves unknown