0
0
SpringbootHow-ToBeginner · 4 min read

How to Use @CachePut in Spring Boot for Cache Updates

In Spring Boot, use @CachePut on a method to update the cache with the method's return value every time it is called, without skipping the method execution. This annotation is useful when you want to refresh cache data after modifying the underlying data source.
📐

Syntax

The @CachePut annotation is placed on a method to update the cache with the method's result. It requires specifying the cache name(s) and optionally a key to identify the cache entry.

  • value: The name of the cache to update.
  • key: (Optional) Expression to compute the cache key.
java
@CachePut(value = "cacheName", key = "#key")
public ReturnType methodName(Parameters) {
    // method logic
}
💻

Example

This example shows a Spring Boot service method that updates a user's information and refreshes the cache entry for that user using @CachePut.

java
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    // Simulate a user repository
    private final Map<Long, String> userDatabase = new HashMap<>();

    public UserService() {
        userDatabase.put(1L, "Alice");
    }

    @CachePut(value = "users", key = "#userId")
    public String updateUser(Long userId, String newName) {
        // Update the "database"
        userDatabase.put(userId, newName);
        // Return the updated user name to update the cache
        return newName;
    }

    public String getUser(Long userId) {
        return userDatabase.get(userId);
    }
}
⚠️

Common Pitfalls

  • Using @CachePut without specifying the correct cache name causes cache updates to fail silently.
  • Not returning the updated value from the method means the cache will store null or stale data.
  • Confusing @CachePut with @Cacheable: @CachePut always executes the method and updates cache, while @Cacheable may skip method execution if cache exists.
java
/* Wrong: Missing return value update */
@CachePut(value = "users", key = "#userId")
public void updateUserWrong(Long userId, String newName) {
    userDatabase.put(userId, newName);
    // No return, cache stores null
}

/* Right: Return updated value */
@CachePut(value = "users", key = "#userId")
public String updateUserRight(Long userId, String newName) {
    userDatabase.put(userId, newName);
    return newName;
}
📊

Quick Reference

@CachePut updates the cache with the method's return value every time the method runs. Use it when you want to refresh cache after data changes.

  • value: Cache name(s) to update.
  • key: Cache key expression (optional).
  • Method must return the updated value to store in cache.
  • Does not skip method execution.

Key Takeaways

Use @CachePut to update cache entries every time a method executes.
Always return the updated value from the method annotated with @CachePut.
Specify the correct cache name and key to ensure proper cache updates.
@CachePut differs from @Cacheable by not skipping method execution.
Use @CachePut when you want to refresh cache after modifying data.