0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

@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

  1. The Spring Cache Abstraction
  2. @Cacheable, @CachePut, @CacheEvict
  3. Redis as a Cache Provider
  4. Cache TTL and Invalidation
← Back to Spring Boot 4 Microservices & REST APIs