@Cacheable, @CachePut, @CacheEvict
Control caching behavior precisely.
The Three Cache Annotations
Spring offers three method-level cache annotations:
@Cacheable— read-through, store the result@CachePut— always run, update the cache@CacheEvict— remove entries
@Cacheable
@Cacheable checks the cache first; on a miss it runs the method and stores the result.
@Cacheable("products")
public Product findById(Long id) {
return repository.findById(id).orElseThrow();
}All lessons in this course
- The Spring Cache Abstraction
- @Cacheable, @CachePut, @CacheEvict
- Redis as a Cache Provider
- Cache TTL and Invalidation