Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to enable caching in a Spring Boot application.
Spring Boot
@Configuration @[1] public class CacheConfig { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("items"); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @EnableScheduling instead of @EnableCaching
Forgetting to add @EnableCaching on a configuration class
✗ Incorrect
The @EnableCaching annotation activates Spring's annotation-driven cache management capability.
2fill in blank
mediumComplete the method annotation to cache the result of the method.
Spring Boot
public class ProductService { @[1]("products") public Product getProductById(Long id) { // method implementation } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @CachePut when @Cacheable is needed
Not specifying the cache name in the annotation
✗ Incorrect
The @Cacheable annotation tells Spring to cache the result of the method using the specified cache name.
3fill in blank
hardFix the error in the cache eviction annotation to clear the cache named 'users'.
Spring Boot
public class UserService { @CacheEvict(cacheNames = [1]) public void clearUserCache() { // method implementation } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the cache name
Using singular instead of plural cache name
✗ Incorrect
The cacheNames attribute requires the cache name as a string literal, so it must be quoted.
4fill in blank
hardFill both blanks to create a cache manager bean that uses a simple concurrent map cache.
Spring Boot
@Bean
public CacheManager cacheManager() {
return new [1]([2]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SimpleCacheManager without configuring caches
Passing cache name without quotes
✗ Incorrect
ConcurrentMapCacheManager is a simple cache manager using concurrent maps. The cache name must be a string.
5fill in blank
hardFill all three blanks to define a cacheable method that caches by product ID and evicts cache on update.
Spring Boot
public class ProductService { @Cacheable(cacheNames = [1], key = "#[2]") public Product getProduct(Long [2]) { // fetch product } @CacheEvict(cacheNames = [1], key = "#[2]") public void updateProduct(Long [2]) { // update product } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different cache names for caching and eviction
Mismatching key parameter names
✗ Incorrect
The cache name is 'products'. The key uses the method parameter named 'id' to cache and evict the correct entry.