0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

@Cacheable, @CachePut, @CacheEvict

Control caching behavior precisely.

@Cacheable, @CachePut, @CacheEvict is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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();
}

Conditional Caching

Use condition to cache only when a predicate holds, and unless to skip caching certain results.

@Cacheable(value = "products",
    unless = "#result == null")
public Product findById(Long id) { ... }

@CachePut

@CachePut always executes the method and stores the returned value — ideal for update methods that should refresh the cache.

@CachePut(value = "products", key = "#product.id")
public Product update(Product product) {
    return repository.save(product);
}

Cacheable vs CachePut

@Cacheable may skip the method on a hit; @CachePut never skips. Never put both on the same method — their behaviors conflict.

@CacheEvict

@CacheEvict removes an entry, typically after a delete or update that invalidates cached data.

@CacheEvict(value = "products", key = "#id")
public void delete(Long id) {
    repository.deleteById(id);
}

Evicting Everything

Use allEntries = true to clear an entire cache at once.

@CacheEvict(value = "products", allEntries = true)
public void reloadAll() {
    // bulk refresh
}

Evict Before or After

By default eviction happens after the method succeeds. Set beforeInvocation = true to evict even if the method throws.

@CacheEvict(value = "products",
    allEntries = true, beforeInvocation = true)
public void risky() { ... }

Grouping with @Caching

@Caching combines multiple cache operations on one method.

@Caching(
    put = @CachePut(value = "products", key = "#p.id"),
    evict = @CacheEvict(value = "productList",
        allEntries = true))
public Product save(Product p) {
    return repository.save(p);
}

Class-Level Config

@CacheConfig sets a default cache name for all methods in a class, reducing repetition.

@Service
@CacheConfig(cacheNames = "products")
public class ProductService {
    @Cacheable(key = "#id")
    public Product findById(Long id) { ... }
}

Self-Invocation Caveat

Cache annotations work via proxies, so a method calling another cached method in the same class bypasses the cache. Call through a separate bean.

Quick Check

Test your understanding of the cache annotations.

Recap

You learned the three cache annotations:

  • @Cacheable — store and reuse results
  • @CachePut — always run and refresh
  • @CacheEvict — remove entries (allEntries, beforeInvocation)
  • Watch out for the self-invocation proxy caveat

Frequently asked questions

Is the “@Cacheable, @CachePut, @CacheEvict” lesson free?

Yes — the full text of “@Cacheable, @CachePut, @CacheEvict” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “@Cacheable, @CachePut, @CacheEvict”?

Control caching behavior precisely. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “@Cacheable, @CachePut, @CacheEvict” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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