How to Configure Cache in Spring Boot: Simple Guide
To configure cache in Spring Boot, enable caching by adding
@EnableCaching to your main application class and use @Cacheable on methods to cache their results. You can customize cache behavior by defining a CacheManager bean and configuring cache properties in application.properties.Syntax
@EnableCaching activates Spring's annotation-driven cache management capability.
@Cacheable marks methods whose results should be cached. You specify a cache name to group cached data.
CacheManager is a Spring bean that controls cache creation and management.
Cache configuration can be done in application.properties for cache provider settings.
java
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CacheConfig { // Optional: define CacheManager bean here } // Usage in a service class public class ProductService { @Cacheable("products") public Product getProductById(Long id) { // method logic return null; // placeholder return } }
Example
This example shows a Spring Boot application with caching enabled. The getProductById method caches its result so repeated calls with the same ID return cached data without executing the method again.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @SpringBootApplication @EnableCaching public class CacheDemoApplication { public static void main(String[] args) { SpringApplication.run(CacheDemoApplication.class, args); } } @Service class ProductService { @Cacheable("products") public String getProductById(Long id) { System.out.println("Fetching product " + id); return "Product-" + id; } } // Running this service method twice with the same id will print "Fetching product 1" only once.
Output
Fetching product 1
// Second call returns cached result without printing
Common Pitfalls
- Forgetting to add
@EnableCachingdisables caching. - Not specifying cache names in
@Cacheablecauses errors. - Using mutable objects as cache keys can cause unexpected cache misses.
- Not configuring a cache provider leads to default in-memory cache which may not suit production.
java
/* Wrong: Missing @EnableCaching disables caching */ @SpringBootApplication public class App { } /* Right: Enable caching */ @SpringBootApplication @EnableCaching public class App { } /* Wrong: No cache name */ @Cacheable public String getData() { return "data"; } /* Right: Specify cache name */ @Cacheable("dataCache") public String getData() { return "data"; }
Quick Reference
- @EnableCaching: Enable caching support.
- @Cacheable("cacheName"): Cache method results.
- CacheManager: Customize cache behavior.
- application.properties: Configure cache provider settings.
Key Takeaways
Add @EnableCaching to your Spring Boot application to activate caching.
Use @Cacheable with a cache name on methods to cache their results.
Configure CacheManager bean to customize cache behavior and storage.
Set cache provider properties in application.properties for production readiness.
Avoid mutable cache keys and always specify cache names to prevent errors.