Performance: @CachePut for updating cache
This affects server response time and memory usage by controlling how cache updates happen during method execution.
@CachePut(value = "items", key = "#item.id") public Item updateItem(Item item) { // update logic return item; }
@Cacheable("items") public Item updateItem(Item item) { // update logic return item; }
| Pattern | Method Execution | Cache Update | Data Freshness | Verdict |
|---|---|---|---|---|
| @Cacheable only | Skipped if cached | No update | Stale data possible | [X] Bad |
| @CachePut | Always runs | Updates cache after | Fresh data guaranteed | [OK] Good |
@CachePut annotation in Spring Boot?@CachePut to update a cache named "users" with a key based on the method parameter id?@CachePut(value = "products", key = "#product.id")
public Product updateProduct(Product product) {
product.setPrice(product.getPrice() + 10);
return product;
}
What will be the effect on the cache when this method is called with a product having id=5 and price=100?@CachePut:
@CachePut(value = "orders", key = "#order.id")
public Order updateOrder(Order order) {
// missing return statement
}
What is the problem with this code?@CachePut to achieve this, assuming the method signature is public User updateEmail(Long userId, String newEmail)?Pass the test and this pattern moves unknown