The @CachePut annotation makes a Spring method always execute and then stores its return value in a cache under the configured key. Its significance is that it updates the cache as a side effect of a write operation, so subsequent @Cacheable reads see the new value immediately instead of reloading it from the database.
This is the difference from @Cacheable, which skips the method on a cache hit. With @CachePut the method body is never skipped.
When a method annotated with @CachePut is called through its Spring proxy:
If the method throws, nothing is written. The condition attribute is checked before invocation and unless is checked after, using the result.
Enable caching, then pair a read method with an update method that uses the same cache name and key:
Calling update(user) saves the entity and refreshes the users cache entry with the saved result. A later findById(user.getId()) returns it from cache without touching the database.
Note that the key must match. Here findById caches by the Long id and update caches by #user.id, which is also a Long, so both address the same entry.
| Attribute | Purpose |
|---|---|
cacheNames / value | Target cache or caches. |
key | SpEL expression for the cache key. |
keyGenerator | Custom key generator bean, as an alternative to key. |
condition | Evaluated before the call; if false, the cache is not updated. |
unless | Evaluated after the call; if true, the result is not cached. |
SpEL gives you access to parameters (#user.id, #p0), the result (#result) and method details (#root.methodName):
Using #result is handy when the key only exists after saving, for example a generated id:
| Annotation | Method runs on hit? | Effect on cache |
|---|---|---|
@Cacheable | No | Stores the result on a miss only. |
@CachePut | Yes, always | Overwrites the entry with the result. |
@CacheEvict | Yes, always | Removes entries. |
Choose @CachePut when you can cheaply produce the up-to-date value as part of the write. Choose @CacheEvict when the new value is expensive or when several cached views depend on the changed data and are easier to invalidate than to recompute.
Do not put @Cacheable and @CachePut on the same method. Spring documents this as unsupported in practice: the @Cacheable hit would skip the method that @CachePut needs to run, and the combination behaves in confusing ways.
void, @CachePut would cache null. Return the entity you want cached.findById returns User and the update method returns a UserDto, readers will get a ClassCastException or the wrong type. Cache the same type under a key.@EnableCaching, which silently disables all caching annotations.No. The method always runs, and the cache entry is overwritten with the new result.
Yes, as long as both use the same cache name and an identical key.
The cache is not updated, because the return value is only stored after normal completion.
Use @CachePut when the saved result is the exact object readers need. Use @CacheEvict when you would rather reload lazily or invalidate several related caches.
@CachePut runs the method every time and writes the result to the cache, keeping reads in sync after writes. Use matching cache names and keys with @Cacheable, return the object you want cached, use unless to skip nulls, and never combine it with @Cacheable on the same method.