Consider a Spring Boot application using Redis as a cache provider with a TTL (time-to-live) set on cached entries.
What will happen when the TTL expires for a cached key?
Think about how Redis handles keys with expiration times.
Redis automatically deletes keys when their TTL expires. This means the key no longer exists, so the application will fetch fresh data and cache it again.
In a Spring Boot application, you want to enable Redis caching support. Which annotation should you add to your main application class?
Look for the general Spring annotation that activates caching.
@EnableCaching is the standard Spring annotation to enable caching abstraction, which works with Redis when configured.
Given this Spring Boot method with Redis caching:
@Cacheable(value = "items") public ListgetItems() { return List.of("apple", "banana"); }
But the cache never stores the list, and the method runs every time. What is the likely cause?
Check if the caching infrastructure is set up correctly.
If Redis cache manager is not configured or missing, caching annotations have no effect and methods run every time.
In Spring Boot Redis caching, what is the main difference between @CachePut and @Cacheable annotations on a method?
Think about when the method runs and when the cache updates.
@CachePut always runs the method and updates the cache with the result. @Cacheable skips method execution if the cache has a value.
Consider this Spring Boot service using Redis cache:
@Cacheable(value = "numbers", key = "#num")
public int square(int num) {
return num * num;
}
// Calls:
int a = square(3);
int b = square(3);
int c = square(4);What are the values of a, b, and c, and how many times is the method body executed?
Remember how @Cacheable caches results by key.
The first call with 3 computes and caches 9. The second call with 3 returns cached 9 without running method. The call with 4 computes and caches 16. So method runs twice.