Spring Boot Spring Boot Caching and Messaging 1 — Questions and Answers
Question 1: Which annotation enables Spring's annotation-driven caching infrastructure in a Spring Boot application?
- @EnableCaching (Correct answer)
- @CacheConfig
- @CacheManagement
- @EnableCache
Correct answer: @EnableCaching
`@EnableCaching` activates the post-processor that detects `@Cacheable`, `@CacheEvict`, and `@CachePut` annotations on Spring beans.
Question 2: What does the `@Cacheable` annotation do when applied to a Spring service method?
- Evicts all entries from the named cache
- Caches the method return value using the method arguments as the key (Correct answer)
- Forces a cache update on every invocation
- Configures the cache TTL for the method
Correct answer: Caches the method return value using the method arguments as the key
`@Cacheable` intercepts the method call, checks the named cache for an existing entry, and only invokes the method on a cache miss.
Question 3: Which Spring Boot starter auto-configures Redis as a cache provider?
- spring-boot-starter-cache
- spring-boot-starter-redis
- spring-boot-starter-data-redis (Correct answer)
- spring-boot-starter-redis-cache
Correct answer: spring-boot-starter-data-redis
Adding `spring-boot-starter-data-redis` and `spring-boot-starter-cache` together causes Spring Boot to auto-configure `RedisCacheManager` as the default `CacheManager`.
Question 4: What is the purpose of the `@CacheEvict` annotation?
- Populates the cache on application startup
- Removes one or all entries from a named cache (Correct answer)
- Marks a method to bypass the cache entirely
- Configures the serialization strategy for cached objects
Correct answer: Removes one or all entries from a named cache
`@CacheEvict` triggers removal of specific entries (or all entries with `allEntries=true`) from the named cache when the annotated method is called.
Question 5: Which Spring Boot property sets the default Time-To-Live for entries in a Redis cache?
- spring.cache.redis.ttl
- spring.redis.cache.expire
- spring.cache.redis.time-to-live (Correct answer)
- spring.data.redis.ttl
Correct answer: spring.cache.redis.time-to-live
`spring.cache.redis.time-to-live` configures the duration after which cache entries are automatically expired from Redis.
Question 6: What does `@CachePut` do differently from `@Cacheable`?
- It evicts the cache before invoking the method
- It always executes the method and updates the cache with the result (Correct answer)
- It stores the method input instead of the output in the cache
- It disables caching for the annotated method
Correct answer: It always executes the method and updates the cache with the result
Unlike `@Cacheable`, `@CachePut` never skips method execution — it always runs the method and writes the result to the cache, useful for updates.
Which annotation enables Spring's annotation-driven caching infrastructure in a Spring Boot application?