0
0
Spring Bootframework~20 mins

Redis as cache provider in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a cached value expires in Redis?

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?

AThe cached key is moved to a backup Redis database automatically.
BThe cached key remains in Redis but returns a null value until manually deleted.
CRedis throws an error when trying to access an expired key.
DThe cached key is automatically removed from Redis, so the next request will fetch fresh data and cache it again.
Attempts:
2 left
💡 Hint

Think about how Redis handles keys with expiration times.

📝 Syntax
intermediate
1:30remaining
Which annotation enables Redis caching in Spring Boot?

In a Spring Boot application, you want to enable Redis caching support. Which annotation should you add to your main application class?

A@RedisCacheable
B@EnableCaching
C@EnableRedisCache
D@CacheConfig
Attempts:
2 left
💡 Hint

Look for the general Spring annotation that activates caching.

🔧 Debug
advanced
2:30remaining
Why does this Redis cache not store values correctly?

Given this Spring Boot method with Redis caching:

@Cacheable(value = "items")
public List getItems() {
    return List.of("apple", "banana");
}

But the cache never stores the list, and the method runs every time. What is the likely cause?

ARedis does not support caching lists.
BThe @Cacheable annotation requires a key attribute to work.
CThe Redis cache manager is not configured properly or missing in the application context.
DThe method must be static to be cached.
Attempts:
2 left
💡 Hint

Check if the caching infrastructure is set up correctly.

🧠 Conceptual
advanced
2:00remaining
What is the effect of using @CachePut instead of @Cacheable with Redis?

In Spring Boot Redis caching, what is the main difference between @CachePut and @Cacheable annotations on a method?

A@CachePut always executes the method and updates the cache, while @Cacheable skips method execution if cache exists.
B@CachePut caches only null values, @Cacheable caches non-null values.
C@CachePut disables Redis caching, @Cacheable enables it.
D@CachePut caches the method result only on exceptions, @Cacheable caches on success.
Attempts:
2 left
💡 Hint

Think about when the method runs and when the cache updates.

state_output
expert
3:00remaining
What is the output after these Redis cache operations in Spring Boot?

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?

Aa=9, b=9, c=16; method executed 2 times
Ba=9, b=9, c=16; method executed 3 times
Ca=9, b=0, c=16; method executed 3 times
Da=9, b=9, c=16; method executed 1 time
Attempts:
2 left
💡 Hint

Remember how @Cacheable caches results by key.