0
0
Spring Bootframework~10 mins

@CachePut for updating cache in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @CachePut for updating cache
Method call with @CachePut
Method executes and returns value
Cache updated with new value
Return updated value to caller
When a method annotated with @CachePut is called, it runs the method and updates the cache with the returned value before returning it.
Execution Sample
Spring Boot
@CachePut(value = "users", key = "#user.id")
public User updateUser(User user) {
    // update user in DB
    return user;
}
This method updates a user and refreshes the cache entry for that user.
Execution Table
StepActionInputCache BeforeCache UpdateReturn Value
1Call updateUser with user{id=1, name='Alice'}user{id=1, name='Alice'}{}{}pending
2Execute method: update DB and return useruser{id=1, name='Alice'}{}{}user{id=1, name='Alice'}
3Update cache 'users' with key=1user{id=1, name='Alice'}{}{1: user{id=1, name='Alice'}}user{id=1, name='Alice'}
4Return updated useruser{id=1, name='Alice'}{1: user{id=1, name='Alice'}}{1: user{id=1, name='Alice'}}user{id=1, name='Alice'}
5Call updateUser with user{id=1, name='Alice Smith'}user{id=1, name='Alice Smith'}{1: user{id=1, name='Alice'}}{1: user{id=1, name='Alice'}}pending
6Execute method: update DB and return useruser{id=1, name='Alice Smith'}{1: user{id=1, name='Alice'}}{1: user{id=1, name='Alice'}}user{id=1, name='Alice Smith'}
7Update cache 'users' with key=1user{id=1, name='Alice Smith'}{1: user{id=1, name='Alice'}}{1: user{id=1, name='Alice Smith'}}user{id=1, name='Alice Smith'}
8Return updated useruser{id=1, name='Alice Smith'}{1: user{id=1, name='Alice Smith'}}{1: user{id=1, name='Alice Smith'}}user{id=1, name='Alice Smith'}
9Call updateUser with user{id=2, name='Bob'}user{id=2, name='Bob'}{1: user{id=1, name='Alice Smith'}}{1: user{id=1, name='Alice Smith'}}pending
10Execute method: update DB and return useruser{id=2, name='Bob'}{1: user{id=1, name='Alice Smith'}}{1: user{id=1, name='Alice Smith'}}user{id=2, name='Bob'}
11Update cache 'users' with key=2user{id=2, name='Bob'}{1: user{id=1, name='Alice Smith'}}{1: user{id=1, name='Alice Smith'}, 2: user{id=2, name='Bob'}}user{id=2, name='Bob'}
12Return updated useruser{id=2, name='Bob'}{1: user{id=1, name='Alice Smith'}, 2: user{id=2, name='Bob'}}{1: user{id=1, name='Alice Smith'}, 2: user{id=2, name='Bob'}}user{id=2, name='Bob'}
13No more calls-{1: user{id=1, name='Alice Smith'}, 2: user{id=2, name='Bob'}}{1: user{id=1, name='Alice Smith'}, 2: user{id=2, name='Bob'}}-
💡 No more method calls; cache holds latest updated users.
Variable Tracker
VariableStartAfter Step 3After Step 7After Step 11Final
cache 'users'{}{1: user{id=1, name='Alice'}}{1: user{id=1, name='Alice Smith'}}{1: user{id=1, name='Alice Smith'}, 2: user{id=2, name='Bob'}}{1: user{id=1, name='Alice Smith'}, 2: user{id=2, name='Bob'}}
return valueN/Auser{id=1, name='Alice'}user{id=1, name='Alice Smith'}user{id=2, name='Bob'}user{id=2, name='Bob'}
Key Moments - 2 Insights
Why does the cache update even if the method is called multiple times with the same key?
Because @CachePut always runs the method and updates the cache with the returned value, as shown in steps 3 and 7 where the cache entry for key=1 is replaced.
Does @CachePut skip method execution if the cache already has the key?
No, @CachePut always executes the method and then updates the cache, unlike @Cacheable which may skip execution if cache hit occurs. This is clear in steps 5-8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 7. What is the cache content after updating user with id=1?
A{1: user{id=1, name='Alice Smith'}}
B{1: user{id=1, name='Alice'}}
C{}
D{2: user{id=2, name='Bob'}}
💡 Hint
Check the 'Cache Update' column at step 7 in the execution table.
At which step does the cache first get a new entry for user id=2?
AStep 9
BStep 11
CStep 10
DStep 12
💡 Hint
Look for when the cache 'users' key=2 appears in the 'Cache Update' column.
If the method returned a different user object but with the same id, what would happen to the cache?
ACache would clear all entries
BCache would keep the old user object
CCache would update with the new user object for that id
DCache would throw an error
💡 Hint
Refer to how cache updates at steps 3 and 7 with new user objects for the same key.
Concept Snapshot
@CachePut runs the method every time and updates the cache with the returned value.
Use it to refresh cache entries after updates.
Unlike @Cacheable, it does not skip method execution.
Specify cache name and key to control cache entry.
Cache is updated after method returns.
Full Transcript
When you use @CachePut on a method, every time you call that method, it runs fully and then updates the cache with the result. For example, if you update a user, the method saves the user to the database and then puts the updated user into the cache under the user's id. This means the cache always has the latest data after the method runs. Unlike @Cacheable, which can skip running the method if the cache already has data, @CachePut always runs the method and refreshes the cache. This is useful when you want to keep the cache in sync after changes. The cache key is usually the id or unique identifier of the data. So, calling updateUser with a changed user will replace the old cached user with the new one. This way, your cache stays fresh and your app uses the latest data.