0
0
Spring Bootframework~10 mins

@EnableCaching annotation in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AEnableTransactionManagement
BEnableScheduling
CEnableAsync
DEnableCaching
Attempts:
3 left
💡 Hint
Common Mistakes
Using @EnableScheduling instead of @EnableCaching
Forgetting to add @EnableCaching on a configuration class
2fill in blank
medium

Complete 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'
ACacheable
BCachePut
CCacheEvict
DCacheConfig
Attempts:
3 left
💡 Hint
Common Mistakes
Using @CachePut when @Cacheable is needed
Not specifying the cache name in the annotation
3fill in blank
hard

Fix 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'
A"user"
Busers
C"users"
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the cache name
Using singular instead of plural cache name
4fill in blank
hard

Fill 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'
AConcurrentMapCacheManager
B"defaultCache"
C"itemsCache"
DSimpleCacheManager
Attempts:
3 left
💡 Hint
Common Mistakes
Using SimpleCacheManager without configuring caches
Passing cache name without quotes
5fill in blank
hard

Fill 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'
A"products"
Bid
C"productId"
D"items"
Attempts:
3 left
💡 Hint
Common Mistakes
Using different cache names for caching and eviction
Mismatching key parameter names