How to Use Caching in Spring Boot: Simple Guide with Examples
To use caching in Spring Boot, add
@EnableCaching to your main class and annotate methods with @Cacheable to store results. Spring Boot manages cache storage automatically, improving performance by avoiding repeated method calls.Syntax
Enable caching by adding @EnableCaching on a configuration or main class. Use @Cacheable on methods whose results you want to cache. Optionally, use @CachePut to update cache and @CacheEvict to remove entries.
@EnableCaching: Activates Spring's cache support.@Cacheable("cacheName"): Caches method return values.@CachePut("cacheName"): Updates cache without skipping method execution.@CacheEvict("cacheName"): Removes cache entries.
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.cache.annotation.CachePut; import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Service; @EnableCaching @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } @Service public class DataService { @Cacheable("items") public String getItem(String id) { // Simulate slow method try { Thread.sleep(2000); } catch (InterruptedException e) {} return "Item-" + id; } @CachePut(value = "items", key = "#id") public String updateItem(String id, String newValue) { return newValue; } @CacheEvict(value = "items", key = "#id") public void removeItem(String id) { // Removes item from cache } }
Example
This example shows a Spring Boot app with caching enabled. The getItem method caches its result, so the first call takes 2 seconds but subsequent calls return instantly from cache.
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) { var context = SpringApplication.run(CacheDemoApplication.class, args); var service = context.getBean(DataService.class); long start = System.currentTimeMillis(); System.out.println(service.getItem("123")); // Takes ~2 seconds System.out.println("First call took: " + (System.currentTimeMillis() - start) + " ms"); start = System.currentTimeMillis(); System.out.println(service.getItem("123")); // Instant from cache System.out.println("Second call took: " + (System.currentTimeMillis() - start) + " ms"); } } @Service class DataService { @Cacheable("items") public String getItem(String id) { try { Thread.sleep(2000); } catch (InterruptedException e) {} return "Item-" + id; } }
Output
Item-123
First call took: 2003 ms
Item-123
Second call took: 1 ms
Common Pitfalls
- Forgetting
@EnableCachingdisables caching. - Using
@Cacheableon private or final methods won't work because Spring proxies can't intercept them. - Cache keys default to method parameters; complex objects may need custom key generators.
- Not configuring a cache manager defaults to simple in-memory cache, which may not suit production.
java
/* Wrong: Missing @EnableCaching - caching won't work */ @SpringBootApplication public class App {} /* Right: Add @EnableCaching to enable caching */ @SpringBootApplication @EnableCaching public class App {}
Quick Reference
| Annotation | Purpose |
|---|---|
| @EnableCaching | Enable Spring caching support |
| @Cacheable | Cache method return values |
| @CachePut | Update cache without skipping method execution |
| @CacheEvict | Remove entries from cache |
Key Takeaways
Add @EnableCaching to your Spring Boot application to activate caching.
Use @Cacheable on methods to cache their results and improve performance.
Remember that caching works only on public methods due to Spring proxies.
Configure a proper cache manager for production use beyond default in-memory cache.
Use @CachePut and @CacheEvict to update or clear cache entries as needed.