Spring's @Cacheable annotation caches the return value of a method: before the method runs, Spring looks up a key in a named cache, returns the stored value on a hit, and otherwise runs the method and stores the result. You enable it with @EnableCaching and tune it with the attributes cacheNames, key, keyGenerator, condition, unless, sync and cacheManager.
The annotation works through a proxy around the bean, which explains most of its limits. Understanding the attributes and the proxy rules is what separates a cache that works from one that quietly does nothing.
@Cacheable examplekey and keyGeneratorcondition and unlesssync@Cacheable exampleEnable it once in the application:
With a single parameter, Spring uses that argument as the key. With no parameters it uses a constant key; with several it combines them into a SimpleKey.
key and keyGeneratorThe key attribute takes a SpEL expression evaluated against the method arguments.
Parameter names are available as #name when the code is compiled with the -parameters flag, which Spring Boot's Maven and Gradle plugins enable by default. For repeated key logic, implement KeyGenerator and reference it with keyGenerator = "myGenerator". You cannot set both key and keyGenerator.
condition and unless| Attribute | Evaluated | Can use #result | Effect |
|---|---|---|---|
condition | Before the call | No | If false, the cache is skipped entirely (no lookup, no store) |
unless | After the call | Yes | If true, the result is not stored |
Use condition to restrict caching to some inputs, and unless to avoid storing empty or error-like results.
syncUnder load, many threads can miss the same key together. Setting sync = true lets one thread compute while the others wait for its result.
Synchronization is per cache and per JVM, supports only one cache name and is not compatible with unless.
With several names, Spring checks them in order and returns the first hit. Use cacheManager to route one method to a different provider when your application defines more than one manager bean. You can also place @CacheConfig(cacheNames = "items") on the class to avoid repeating the name.
this.priceFor(sku) inside the same class bypasses the proxy. Call it from another bean.final classes and methods cannot be proxied by CGLIB.equals and hashCode.Return types such as Optional<T> are unwrapped and re-wrapped, and CompletableFuture results are supported by providers that implement async caching, such as Caffeine.
value and cacheNames?They are aliases. cacheNames is the clearer name and is preferred in new code.
@Cacheable cache exceptions?No. If the method throws, nothing is stored and the next call runs the method again.
Yes. Spring uses a default constant key, so the one value is cached until evicted or expired.
Inject CacheManager, call getCache("prices") and then clear(), or use @CacheEvict(allEntries = true).
@Cacheable stores a method's result under a key in a named cache and skips the method on later hits. Control keys with key, apply rules with condition and unless, avoid stampedes with sync, and always call cached methods through a Spring proxy so the cache is actually used.