← All tutorials

GoCareerGo Tutorials

Spring Boot Tutorial

Auto-configuration, REST APIs, JPA, AOP, caching, microservices and Spring Cloud.

@Cacheable, @CachePut, @CacheEvict

Declarative caching annotations that avoid re-running expensive method calls when the same input has already been computed.

@Service
public class ProductService {
    @Cacheable("products")
    public Product findById(Long id) { ... }      // caches the result

    @CachePut(value = "products", key = "#p.id")
    public Product update(Product p) { ... }        // updates the cache

    @CacheEvict(value = "products", key = "#id")
    public void delete(Long id) { ... }              // removes from cache
}