The spring-boot-starter-cache dependency adds Spring's caching abstraction to a Spring Boot application. With @EnableCaching and annotations such as @Cacheable, you can store the result of an expensive method call and reuse it, without writing cache code yourself or tying the application to one cache product.
Spring Boot auto-configures a CacheManager for the cache library it finds on the classpath. If none is found, it falls back to a simple in-memory ConcurrentHashMap, which is fine for development but has no expiry or size limit.
The starter pulls in spring-context-support and the Spring caching infrastructure. It does not include a real cache provider. Spring Boot then picks a provider in a fixed order, including Caffeine, Redis, Hazelcast, Infinispan, Couchbase, JCache (JSR-107) providers such as Ehcache 3, and finally the simple map. The abstraction means your service code stays the same when you swap the provider.
Add the dependency:
Then switch caching on with @EnableCaching on a configuration class or your main class:
Without @EnableCaching the cache annotations are ignored.
| Annotation | What it does |
|---|---|
@Cacheable | Returns the cached value if present, otherwise runs the method and stores the result |
@CachePut | Always runs the method and updates the cache with the result |
@CacheEvict | Removes one entry, or all entries with allEntries = true |
@Caching | Groups several cache operations on one method |
@CacheConfig | Sets shared options, such as cache names, on a class |
This service simulates a slow lookup. The first call for an id takes about a second, and later calls return from the cache.
Calling findById(1) twice logs a slow first call and an instant second call:
The key is a SpEL expression. When you omit it, Spring builds the key from all method arguments. Use condition to cache only some calls and unless to skip storing certain results, for example unless = "#result == null".
Caching is implemented with Spring AOP proxies. This has consequences:
public methods of Spring beans. Annotations on private methods have no effect.For a single instance, Caffeine is a good local cache with size limits and expiry. Add com.github.ben-manes.caffeine:caffeine and configure it:
For multiple application instances, a distributed cache such as Redis keeps all nodes consistent. Add spring-boot-starter-data-redis and set a time to live:
| Provider | Scope | Typical use |
|---|---|---|
Simple (ConcurrentHashMap) | One JVM | Tests, prototypes; no eviction |
| Caffeine | One JVM | Fast local cache with expiry and size limits |
| Redis | Shared | Several instances, survives restarts |
| Hazelcast, Infinispan | Shared or embedded grid | Clustered in-memory data |
If you also use Spring Boot Actuator, the caches endpoint lists cache names and lets you clear them (DELETE /actuator/caches/products) once it is exposed. Micrometer publishes metrics such as cache.gets with hit and miss tags for supported providers, so you can watch the hit ratio. A low hit ratio usually means the keys or time to live need adjusting.
null values by accident: use unless = "#result == null" or disable null values in the provider.@Cacheable(sync = true) so only one caller computes the value.No. Without one Spring Boot uses a simple in-memory map. For production add Caffeine, Redis or another provider that supports expiry.
The usual causes are a missing @EnableCaching, a call from within the same class, a non-public method, or an unstable key.
@Cacheable skips the method when a value exists. @CachePut always runs the method and refreshes the cache entry.
Use @CacheEvict(cacheNames = "products", allEntries = true) on a method, or inject CacheManager and call getCache("products").clear().
The spring-boot-starter-cache dependency gives you provider-independent caching through @Cacheable, @CachePut and @CacheEvict. Enable it with @EnableCaching, pick Caffeine for local caching or Redis for shared caching, and set keys, expiry and eviction deliberately so the cache stays fast and correct.