0
0
Spring Bootframework~20 mins

@Cacheable for read caching in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a method annotated with @Cacheable is called multiple times with the same argument?

Consider a Spring Boot service method annotated with @Cacheable. What is the behavior when this method is called multiple times with the same input?

Spring Boot
public class ProductService {

  @Cacheable("products")
  public Product getProductById(Long id) {
    simulateSlowService();
    return new Product(id, "Product " + id);
  }

  private void simulateSlowService() {
    try {
      Thread.sleep(3000L);
    } catch (InterruptedException e) {
      throw new IllegalStateException(e);
    }
  }
}
AThe method executes only once per unique argument; subsequent calls return cached result instantly.
BThe method executes fully every time, ignoring the cache.
CThe method caches the result but still executes every time.
DThe method throws an exception if called more than once with the same argument.
Attempts:
2 left
💡 Hint

Think about how caching avoids repeated work for the same input.

📝 Syntax
intermediate
2:00remaining
Which @Cacheable annotation syntax correctly caches a method result with a custom cache name and key?

Choose the correct @Cacheable annotation usage to cache a method result in cache named "users" using the method's first argument as the key.

Spring Boot
public User getUserById(Long id) { ... }
A@Cacheable(value = "users", key = "id")
B@Cacheable(cache = "users", key = "#id")
C@Cacheable(cacheNames = "users", key = "#id")
D@Cacheable(cacheNames = "users", key = "id")
Attempts:
2 left
💡 Hint

Remember the SpEL syntax for keys uses # before parameter names.

🔧 Debug
advanced
2:00remaining
Why does the @Cacheable annotation not cache the method result in this Spring service?

Given the following Spring service, why is the caching not working as expected?

Spring Boot
public class OrderService {

  @Cacheable("orders")
  public Order getOrder(Long id) {
    return fetchOrderFromDb(id);
  }

  public Order fetchOrderFromDb(Long id) {
    // Simulate DB call
    return new Order(id, "Order " + id);
  }

  public Order getCachedOrder(Long id) {
    return getOrder(id);
  }
}
AThe fetchOrderFromDb method should be annotated with @Cacheable instead.
BThe method getOrder is called internally by getCachedOrder, so Spring proxy does not intercept the call to apply caching.
CThe cache name "orders" is invalid and causes caching to fail silently.
DThe Order class must implement Serializable for caching to work.
Attempts:
2 left
💡 Hint

Think about how Spring AOP proxies work and method calls inside the same class.

state_output
advanced
2:00remaining
What is the output when calling a @Cacheable method twice with different arguments?

Consider this Spring Boot service method:

  @Cacheable("items")
  public String getItemName(int id) {
    System.out.println("Fetching item " + id);
    return "Item" + id;
  }

What will be printed to the console when calling getItemName(1) and then getItemName(2)?

A
Fetching item 2
Fetching item 1
B
Fetching item 1
(no output for second call)
C(no output for both calls)
D
Fetching item 1
Fetching item 2
Attempts:
2 left
💡 Hint

Remember caching stores results per unique argument.

🧠 Conceptual
expert
2:00remaining
Which statement about @Cacheable behavior is TRUE regarding null return values?

In Spring Boot, when a method annotated with @Cacheable returns null, what happens by default?

ANull values are never cached by default; the method executes every time for null results.
BNull values are cached by default, so subsequent calls return null from cache.
CCaching null values causes a runtime exception unless configured otherwise.
DNull values are cached only if the cache manager supports it explicitly.
Attempts:
2 left
💡 Hint

Think about why caching nulls might be problematic.