Cache configuration helps your app remember data so it can work faster next time. It saves time by not repeating slow tasks.
Cache configuration in Spring Boot
@EnableCaching @Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("items"); } }
@EnableCaching turns on caching in your Spring Boot app.
CacheManager defines where and how cache is stored.
@EnableCaching @Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("books", "authors"); } }
@EnableCaching @Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { SimpleCacheManager manager = new SimpleCacheManager(); manager.setCaches(List.of(new ConcurrentMapCache("users"))); return manager; } }
This program sets up a cache named 'greetings'. The GreetingService method getGreeting simulates a slow call by waiting 2 seconds. The first call caches the result. The second call returns the cached greeting immediately, making it faster.
import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Service; import java.util.List; @EnableCaching @Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("greetings"); } } @Service class GreetingService { @Cacheable("greetings") public String getGreeting(String name) { try { Thread.sleep(2000); // simulate slow method } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return "Hello, " + name + "!"; } } // Usage example (e.g., in a Spring context with @Autowired GreetingService service): // System.out.println(service.getGreeting("Alice")); // takes 2 seconds first time // System.out.println(service.getGreeting("Alice")); // returns instantly from cache
Cache names must match between configuration and @Cacheable annotations.
Use caching only for data that does not change often or can be stale briefly.
Spring Boot supports many cache types like Redis, Ehcache, and simple memory caches.
Cache configuration speeds up apps by storing data temporarily.
Use @EnableCaching and define a CacheManager bean to set up caching.
Mark methods with @Cacheable to save their results automatically.
Practice
Solution
Step 1: Understand caching concept
Caching stores data temporarily to avoid repeated processing or fetching.Step 2: Identify caching benefit in Spring Boot
It speeds up the app by reusing stored data instead of recalculating or fetching again.Final Answer:
To temporarily store data and speed up application performance -> Option AQuick Check:
Cache = Temporary storage for speed [OK]
- Confusing cache with permanent database storage
- Thinking cache encrypts data
- Mixing cache with security features
Solution
Step 1: Recall Spring Boot caching setup
To activate caching, Spring Boot requires a special annotation on a configuration class.Step 2: Identify the correct annotation
@EnableCaching enables the caching mechanism globally in the app.Final Answer:
@EnableCaching -> Option AQuick Check:
@EnableCaching activates caching [OK]
- Using @Cacheable to enable caching (it marks methods, not enable)
- Confusing @CachePut with enabling caching
- Thinking @CacheConfig enables caching globally
getUser(1) is called twice?@Cacheable("users")
public User getUser(int id) {
System.out.println("Fetching user from DB");
return new User(id, "Name" + id);
}Solution
Step 1: Understand @Cacheable behavior
The first call runs the method and caches the result; subsequent calls return cached data.Step 2: Analyze output for two calls
"Fetching user from DB" prints only once on first call; second call uses cache, no print.Final Answer:
Fetching user from DB printed once, second call returns cached User -> Option DQuick Check:
@Cacheable caches after first call [OK]
- Assuming method runs every time ignoring cache
- Thinking cache returns null if not found
- Confusing @Cacheable with @CachePut behavior
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new CacheManager();
}
}Solution
Step 1: Check CacheManager instantiation
CacheManager is an interface; you cannot create it with new CacheManager().Step 2: Correct way to create CacheManager bean
You must return a concrete implementation like ConcurrentMapCacheManager.Final Answer:
CacheManager is an interface and cannot be instantiated directly -> Option BQuick Check:
CacheManager interface needs implementation [OK]
- Trying to instantiate interface directly
- Forgetting to add @EnableCaching
- Thinking @Cacheable is needed on config class
Solution
Step 1: Understand cache expiration support
ConcurrentMapCacheManager does not support TTL (time to live) expiration by default.Step 2: Identify cache manager with TTL support
CaffeineCacheManager supports expiration policies like 10-minute TTL.Step 3: Evaluate options
@Cacheable does not accept expiration parameters; @CachePut updates cache but does not manage expiration automatically.Final Answer:
Use @EnableCaching and configure a CaffeineCacheManager with expiry policy of 10 minutes -> Option CQuick Check:
CaffeineCacheManager supports TTL expiration [OK]
- Assuming ConcurrentMapCacheManager supports TTL
- Trying to set expiration via @Cacheable annotation
- Using @CachePut to handle expiration manually
Ready to prove it?
Pass the test and this pattern moves unknown
