Complete the code to enable caching in a Spring Boot application.
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.[1](Application.class, args); } }
The SpringApplication.run() method starts the Spring Boot application.
Complete the annotation to enable caching in a Spring Boot application.
@Configuration @[1] public class CacheConfig { }
The @EnableCaching annotation activates Spring's annotation-driven cache management capability.
Fix the error in the cacheable method annotation to cache the result.
@Service public class ProductService { @Cacheable(cacheNames = "products", key = "#[1]") public Product getProductById(Long id) { // fetch product } }
The method parameter is named id, so the key expression must use #id to cache by that parameter.
Fill both blanks to configure a cache manager bean using ConcurrentMapCacheManager.
@Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { return new [1](List.of("[2]")); } }
The ConcurrentMapCacheManager is a simple cache manager using a concurrent map. The cache name here is 'products'.
Fill all three blanks to create a cacheable method that caches by user ID and cache name 'users'.
@Service public class UserService { @Cacheable(cacheNames = "[1]", key = "#[2]") public User getUserById(Long [3]) { // fetch user } }
The cache name is 'users'. The method parameter is named 'id', so the key expression uses '#id'. The parameter name in the method must match 'id'.