@Cacheable, @CachePut, @CacheEvict — Spring Technical Interview Questions & Answers

Declarative caching on service methods.

@Cacheable, @CachePut, @CacheEvict

  • Cache annotations declare when to store, update, or remove cached values.
  • @Cacheable: return cached value if present; otherwise run method and store result.
  • @CachePut: always run method and update the cache.
  • @CacheEvict: remove cache entries (after create/update/delete).
  • Caching methods with side effects accidentally.
  • Read=@Cacheable, write path often needs @CacheEvict/@CachePut.

Technical Interview Tip: Explain all three annotations with one line each.

Code Examples for @Cacheable, @CachePut, @CacheEvict

@Cacheable, @CachePut, @CacheEvict — working example

// Topic: @Cacheable, @CachePut, @CacheEvict
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
class TopicController {
  @GetMapping("/api/topic")
  String topic() {
    return "Spring Boot: @Cacheable, @CachePut, @CacheEvict";
  }
}

@Cacheable, @CachePut, @CacheEvict: GET /api/topic returns this title string.

How to Explain @Cacheable, @CachePut, @CacheEvict to an Interviewer

What is @Cacheable, @CachePut, @CacheEvict (sb annotations)?

Used for a short @Cacheable, @CachePut, @CacheEvict sb annotations example a small REST app Not just the heading

Give one small example of @Cacheable, @CachePut, @CacheEvict (sb annotations).

the smallest @Cacheable, @CachePut, @CacheEvict sb annotations example you can type Declarative caching on service methods

What mistake do beginners make with @Cacheable, @CachePut, @CacheEvict (sb annotations)?

only saying “@Cacheable, @CachePut, @CacheEvict sb annotations” with no example Show the fix

Where do you use @Cacheable, @CachePut, @CacheEvict (sb annotations)?

a small REST app a short @Cacheable, @CachePut, @CacheEvict sb annotations example