📖 Scenario: You are building a Spring Boot application that needs to cache user data to improve performance. Caching helps store data temporarily so the app can quickly access it without fetching it repeatedly from the database.
🎯 Goal: Create a simple cache configuration in Spring Boot to cache user information using the built-in caching support.
📋 What You'll Learn
Create a cache manager bean
Enable caching in the Spring Boot application
Use a cacheable method to store user data in the cache
Configure cache properties in the application
💡 Why This Matters
🌍 Real World
Caching is used in real applications to speed up data retrieval and reduce load on databases or external services.
💼 Career
Understanding cache configuration is important for backend developers working with Spring Boot to build efficient and scalable applications.
Progress0 / 4 steps
1
Enable caching in the Spring Boot application
Add the @EnableCaching annotation to the main application class called CacheApplication.
Spring Boot
Hint
The @EnableCaching annotation activates Spring's cache support.
2
Create a cache manager bean
In a new configuration class called CacheConfig, create a method cacheManager() that returns a ConcurrentMapCacheManager bean with cache name users.
Spring Boot
Hint
The ConcurrentMapCacheManager manages caches stored in memory using a concurrent map.
3
Add a cacheable method to store user data
Create a service class called UserService with a method getUserById that takes a Long id parameter and returns a String. Annotate this method with @Cacheable("users") and return a string like "User" + id.
Spring Boot
Hint
The @Cacheable annotation tells Spring to cache the result of this method using the cache named "users".
4
Configure cache properties in application.properties
Add the line spring.cache.cache-names=users to the application.properties file to declare the cache name.
Spring Boot
Hint
This property tells Spring Boot which cache names to recognize and manage.
Practice
(1/5)
1. What is the primary purpose of using cache configuration in a Spring Boot application?
easy
A. To temporarily store data and speed up application performance
B. To permanently save data to the database
C. To encrypt sensitive data before sending it
D. To manage user authentication and authorization
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 A
Quick Check:
Cache = Temporary storage for speed [OK]
Hint: Cache means temporary storage to speed up apps [OK]
Common Mistakes:
Confusing cache with permanent database storage
Thinking cache encrypts data
Mixing cache with security features
2. Which annotation is used to enable caching support in a Spring Boot application?
easy
A. @EnableCaching
B. @Cacheable
C. @CacheConfig
D. @CachePut
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 A
Quick Check:
@EnableCaching activates caching [OK]
Hint: Enable caching with @EnableCaching annotation [OK]
Common Mistakes:
Using @Cacheable to enable caching (it marks methods, not enable)
Confusing @CachePut with enabling caching
Thinking @CacheConfig enables caching globally
3. Given the code below, what will be the output when 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);
}
medium
A. Fetching user from DB printed twice, two User objects returned
B. No output printed, both calls return null
C. Fetching user from DB printed once, but both calls create new User
D. Fetching user from DB printed once, second call returns cached User
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 D
Quick Check:
@Cacheable caches after first call [OK]
Hint: First call runs method, next calls use cache [OK]
Common Mistakes:
Assuming method runs every time ignoring cache
Thinking cache returns null if not found
Confusing @Cacheable with @CachePut behavior
4. Identify the error in this cache configuration snippet:
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new CacheManager();
}
}
medium
A. Missing @Cacheable annotation on methods
B. CacheManager is an interface and cannot be instantiated directly
C. Missing @EnableCaching annotation
D. CacheManager bean method should be static
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 B
Quick Check:
CacheManager interface needs implementation [OK]
Hint: CacheManager is interface; use implementation class [OK]
Common Mistakes:
Trying to instantiate interface directly
Forgetting to add @EnableCaching
Thinking @Cacheable is needed on config class
5. You want to configure a cache in Spring Boot that stores user data and expires entries after 10 minutes. Which approach correctly combines cache configuration and expiration?
hard
A. Use @EnableCaching and define a ConcurrentMapCacheManager bean with TTL set to 10 minutes
B. Use @CachePut and manually remove cache entries after 10 minutes
C. Use @EnableCaching and configure a CaffeineCacheManager with expiry policy of 10 minutes
D. Use @Cacheable with a parameter to set expiration time to 10 minutes
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 C
Quick Check:
CaffeineCacheManager supports TTL expiration [OK]
Hint: Use CaffeineCacheManager for TTL expiration [OK]
Common Mistakes:
Assuming ConcurrentMapCacheManager supports TTL
Trying to set expiration via @Cacheable annotation
Using @CachePut to handle expiration manually
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.