0
0
Spring Bootframework~20 mins

@CachePut for updating cache in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery with @CachePut
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 @CachePut is called?

Consider a Spring Boot method annotated with @CachePut. What is the behavior when this method is executed?

AThe method executes but does not update the cache.
BThe method does not execute; the cached value is returned directly.
CThe cache is cleared before the method executes.
DThe method executes and updates the cache with the returned value without skipping the method execution.
Attempts:
2 left
💡 Hint

Think about whether the method runs or if the cache is just read.

📝 Syntax
intermediate
2:00remaining
Which @CachePut annotation syntax correctly updates cache named 'users' with key 'user.id'?

Choose the correct @CachePut annotation syntax to update the cache named users using the id property of the user parameter as the key.

A@CachePut(value = "users", key = "user.id")
B@CachePut(value = "users", key = "#user.id")
C@CachePut(cacheNames = "users", key = "user.id")
D@CachePut(cacheNames = "users", key = "#user")
Attempts:
2 left
💡 Hint

Remember to use SpEL (Spring Expression Language) syntax for keys.

🔧 Debug
advanced
2:00remaining
Why does the cache not update after calling a @CachePut method?

Given this method:

  @CachePut(value = "products", key = "#product.id")
  public Product updateProduct(Product product) {
    productRepository.save(product);
    return product;
  }

Why might the cache not update after calling updateProduct?

AThe method is called from within the same class, so Spring proxy does not intercept and update cache.
BThe <code>productRepository.save(product)</code> does not update the database, so cache update is skipped.
CThe method returns the same product instance without changes, so cache sees no update.
DThe cache name 'products' is invalid and causes silent failure.
Attempts:
2 left
💡 Hint

Think about how Spring AOP proxies work with self-invocation.

state_output
advanced
2:00remaining
What is the cache content after calling updateUser twice with different data?

Given this method:

  @CachePut(value = "users", key = "#user.id")
  public User updateUser(User user) {
    return user;
  }

And these calls:

  updateUser(new User(1, "Alice"));
  updateUser(new User(1, "Bob"));

What is the cached value for key 1?

AUser object with id=1 and name='Bob'
BUser object with id=1 and name='Alice'
CCache is empty because @CachePut does not store values
DCache contains both Alice and Bob objects under key 1
Attempts:
2 left
💡 Hint

Consider what happens when the same key is updated twice.

🧠 Conceptual
expert
2:00remaining
Why use @CachePut instead of @Cacheable when updating data?

Which reason best explains why @CachePut is preferred over @Cacheable for methods that update data?

A@CachePut skips method execution if cache has the key, improving performance.
B@CachePut only caches null results to reduce cache size.
C@CachePut always executes the method and updates the cache, ensuring cache consistency after data changes.
D@CachePut clears the entire cache before method execution to avoid stale data.
Attempts:
2 left
💡 Hint

Think about cache consistency when data changes.