What is the purpose of the @CachePut annotation in Spring Boot?
The @CachePut annotation updates the cache with the method's result every time the method is called, without skipping the method execution.
Click to reveal answer
beginner
How does @CachePut differ from @Cacheable in Spring Boot caching?
@Cacheable skips method execution if the cache has the value, while @CachePut always executes the method and updates the cache with the new result.
Click to reveal answer
beginner
Which attribute of @CachePut specifies the cache name to update?
The 'value' or 'cacheNames' attribute specifies the cache name where the result will be stored or updated.
Click to reveal answer
intermediate
Can @CachePut be used to update multiple caches at once?
Yes, by specifying multiple cache names in the 'value' or 'cacheNames' attribute as an array, @CachePut can update multiple caches simultaneously.
Click to reveal answer
intermediate
Why is @CachePut useful when updating data in a database?
Because it ensures the cache is updated with the latest data after a database update, keeping cache and database in sync without skipping method execution.
Click to reveal answer
What happens when a method annotated with @CachePut is called?
AThe method executes and the cache is updated with the result.
BThe method is skipped if cache has the value.
CThe cache is cleared before method execution.
DThe cache is never updated.
✗ Incorrect
@CachePut always executes the method and updates the cache with the new result.
Which attribute do you use to specify the cache name in @CachePut?
AcacheKey
BcacheName
Cvalue
DcacheStore
✗ Incorrect
The 'value' or 'cacheNames' attribute specifies the cache name in @CachePut.
Can @CachePut be used to update the cache without executing the method?
AYes, it skips method execution.
BOnly with a special flag.
COnly if cache is empty.
DNo, it always executes the method.
✗ Incorrect
@CachePut always executes the method and then updates the cache.
Why might you choose @CachePut over @Cacheable when updating data?
ATo update cache with fresh data after changes.
BTo avoid method execution.
CTo clear the cache.
DTo disable caching.
✗ Incorrect
@CachePut updates the cache with fresh data after method execution, useful when data changes.
How do you update multiple caches with @CachePut?
AUse multiple @CachePut annotations.
BSpecify multiple cache names in the 'value' attribute as an array.
CCall the method multiple times.
DIt is not possible.
✗ Incorrect
You can specify multiple cache names in the 'value' or 'cacheNames' attribute as an array.
Explain how @CachePut works and when you would use it in a Spring Boot application.
Think about keeping cache and database data in sync.
You got /4 concepts.
Describe the difference between @CachePut and @Cacheable annotations in Spring Boot caching.
Focus on method execution and cache update differences.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the @CachePut annotation in Spring Boot?
easy
A. To update the cache with the method's return value after execution
B. To clear all entries in the cache before method execution
C. To cache the method result only if it is not already cached
D. To disable caching for the annotated method
Solution
Step 1: Understand the role of @CachePut
@CachePut is used to update the cache with fresh data after the method runs, unlike @Cacheable which caches only if data is absent.
Step 2: Compare options with @CachePut behavior
Only To update the cache with the method's return value after execution correctly describes updating the cache after method execution. Others describe different caching behaviors.
Final Answer:
To update the cache with the method's return value after execution -> Option A
Quick Check:
@CachePut updates cache after method runs [OK]
Hint: Remember: @CachePut always updates cache after method runs [OK]
Common Mistakes:
Confusing @CachePut with @Cacheable
Thinking @CachePut clears cache
Assuming it caches only if absent
2. Which of the following is the correct syntax to use @CachePut to update a cache named "users" with a key based on the method parameter id?
easy
A. @CachePut(cacheName = "users", key = "id")
B. @CachePut(value = "users", key = "#id")
C. @CachePut(name = "users", key = "id")
D. @CachePut(cache = "users", key = "#id")
Solution
Step 1: Recall correct attribute names for @CachePut
The correct attribute for cache name is 'value' or 'cacheNames', and SpEL expressions for keys use '#'.
Step 2: Analyze each option's syntax
@CachePut(value = "users", key = "#id") uses 'value' and '#id' correctly. Others use incorrect attribute names or miss the '#' for SpEL.
Final Answer:
@CachePut(value = "users", key = "#id") -> Option B
Quick Check:
Use 'value' and '#param' for key in @CachePut [OK]
Hint: Use 'value' for cache name and '#' before param in key [OK]
Common Mistakes:
Using wrong attribute names like cacheName or cache
Missing '#' in key expression
Using plain string instead of SpEL for key
3. Given the following method in a Spring Boot service:
What will be the effect on the cache when this method is called with a product having id=5 and price=100?
medium
A. The cache is not affected because @CachePut only caches if absent
B. The cache entry with key 5 is removed from the cache
C. The cache entry with key 5 is updated with the product having price 110
D. A new cache entry with key 110 is added
Solution
Step 1: Understand @CachePut behavior on method call
@CachePut updates the cache entry with the key specified after the method returns the updated object.
Step 2: Analyze the method logic and cache key
The method increases price by 10 and returns the product. The key is product.id which is 5, so cache key 5 is updated with product price 110.
Final Answer:
The cache entry with key 5 is updated with the product having price 110 -> Option C
Quick Check:
@CachePut updates cache key with returned value [OK]
Hint: Returned value updates cache at key specified [OK]
Common Mistakes:
Thinking cache key changes to price
Assuming cache entry is removed
Confusing @CachePut with @Cacheable
4. Consider this method annotated with @CachePut:
@CachePut(value = "orders", key = "#order.id")
public Order updateOrder(Order order) {
// missing return statement
}
What is the problem with this code?
medium
A. The method should be annotated with @Cacheable instead
B. The key expression is invalid and causes a syntax error
C. The cache name 'orders' is not allowed in @CachePut
D. The method does not return a value, so cache cannot be updated
Solution
Step 1: Check method return requirement for @CachePut
@CachePut requires the method to return a value to update the cache with that value.
Step 2: Identify missing return statement issue
The method lacks a return statement, so no value is returned to update the cache, causing runtime issues.
Final Answer:
The method does not return a value, so cache cannot be updated -> Option D
Quick Check:
@CachePut needs method return to update cache [OK]
Hint: Always return updated value when using @CachePut [OK]
Common Mistakes:
Forgetting to return the updated object
Confusing cache name attribute
Using @Cacheable instead of @CachePut incorrectly
5. You have a method that updates a user's email and you want to ensure the cache entry for that user is updated immediately after the change. Which approach correctly uses @CachePut to achieve this, assuming the method signature is public User updateEmail(Long userId, String newEmail)?
hard
A. @CachePut(value = "users", key = "#userId")
public User updateEmail(Long userId, String newEmail) {
User user = userRepository.findById(userId);
user.setEmail(newEmail);
userRepository.save(user);
return user;
}
B. @CachePut(value = "users", key = "#newEmail")
public User updateEmail(Long userId, String newEmail) {
User user = userRepository.findById(userId);
user.setEmail(newEmail);
userRepository.save(user);
return user;
}
C. @CachePut(value = "users")
public void updateEmail(Long userId, String newEmail) {
User user = userRepository.findById(userId);
user.setEmail(newEmail);
userRepository.save(user);
}
D. @CachePut(key = "#userId")
public User updateEmail(Long userId, String newEmail) {
User user = userRepository.findById(userId);
user.setEmail(newEmail);
userRepository.save(user);
return user;
}
Solution
Step 1: Verify correct cache name and key usage
@CachePut(value = "users", key = "#userId")
public User updateEmail(Long userId, String newEmail) {
User user = userRepository.findById(userId);
user.setEmail(newEmail);
userRepository.save(user);
return user;
} uses 'value = "users"' and key '#userId', correctly identifying the cache and key.
Step 2: Check method return and update logic
@CachePut(value = "users", key = "#userId")
public User updateEmail(Long userId, String newEmail) {
User user = userRepository.findById(userId);
user.setEmail(newEmail);
userRepository.save(user);
return user;
} returns the updated User object, which is necessary for @CachePut to update the cache. Other options either use wrong key, no return, or missing cache name.
Final Answer:
@CachePut(value = "users", key = "#userId")
public User updateEmail(Long userId, String newEmail) {
User user = userRepository.findById(userId);
user.setEmail(newEmail);
userRepository.save(user);
return user;
} -> Option A
Quick Check:
Return updated object and use correct cache name and key [OK]
Hint: Return updated object and use correct cache name and key [OK]
Common Mistakes:
Using wrong key expression
Not returning updated object
Omitting cache name in annotation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.