@EnableCaching turns on Spring's annotation-driven cache support. After you add it to a configuration class, annotations such as @Cacheable, @CachePut and @CacheEvict start working: Spring wraps the affected beans in proxies that check a cache before running a method and store the result afterwards. Without it, those annotations are ignored.
The annotation only enables the mechanism. The actual storage comes from a CacheManager, which can be a simple in-memory map, Caffeine, Redis or another provider.
@EnableCaching activates@EnableCaching activates@EnableCaching registers the caching interceptor and advisor (CacheInterceptor) that Spring uses to apply cache annotations through proxies. In Spring Boot it also triggers CacheAutoConfiguration, which detects a provider on the classpath and creates a matching CacheManager.
In Boot, put it on a @Configuration class or on your @SpringBootApplication class. Add spring-boot-starter-cache for the abstraction and, if you want, a provider such as Caffeine.
Here Product and ProductRepository are your own types. The behaviour of each annotation:
| Annotation | Behaviour |
|---|---|
@Cacheable | Returns the cached value if present; otherwise runs the method and caches the result |
@CachePut | Always runs the method and stores the result, to keep the cache fresh |
@CacheEvict | Removes one entry, or all entries with allEntries = true |
@Caching | Groups several cache operations on one method |
@CacheConfig | Shares settings such as cacheNames across a class |
Use condition and unless to control when to cache, for example unless = "#result == null".
Spring Boot checks for providers in a fixed order and uses the first it finds, unless you set spring.cache.type. If none is present, it falls back to a ConcurrentMapCacheManager, which keeps unbounded entries in memory and never expires them.
For production, pick a provider that supports size limits and expiry:
Caffeine suits a single instance. For a cache shared across several instances, use Redis and set spring.cache.redis.time-to-live. Expiry is a provider feature, not part of the Spring abstraction itself.
public for the default proxy mode.key explicitly when arguments are complex objects without a good equals and hashCode.null result is cached by default unless the provider disallows it or you use unless.@CachePut or @CacheEvict.@Cacheable does not stop concurrent misses from calling the method twice. Set sync = true to make one thread compute while others wait.spring.cache.type=none turns it off.@EnableCaching in Spring Boot?Yes. Boot's cache auto-configuration only applies once caching is enabled, either by this annotation or an equivalent configuration.
@Cacheable method called every time?Usually the call is a self-invocation, the annotation is missing from the called method, @EnableCaching is absent, or each call produces a different key.
No. Expiry depends on the provider, such as Caffeine or Redis settings.
Spring Framework 6.1 and later can cache CompletableFuture-returning and reactive methods, caching the resolved value rather than the future.
@EnableCaching activates Spring's cache proxies so @Cacheable, @CachePut and @CacheEvict take effect. Choose a real provider with size and expiry limits, keep write paths in sync with the cache, and avoid self-invocation.