0
0
Spring Bootframework~10 mins

@CacheEvict for invalidation in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @CacheEvict for invalidation
Method Called
Check @CacheEvict
Evict Cache Entry
Execute Method Body
Return Result
Cache Updated Without Evicted Entry
When a method with @CacheEvict runs, it first removes specified cache entries, then runs the method, ensuring stale data is cleared.
Execution Sample
Spring Boot
@CacheEvict(value = "books", key = "#id", beforeInvocation = true)
public void deleteBook(Long id) {
  bookRepository.deleteById(id);
}
This method deletes a book by id and removes its cache entry from 'books' cache.
Execution Table
StepActionCache State BeforeCache EvictionMethod ExecutionCache State After
1Method deleteBook(42) called{"books": {"42": "Book A", "43": "Book B"}}Evict key 42 from 'books'Delete book with id 42 from DB{"books": {"43": "Book B"}}
2Method returns{"books": {"43": "Book B"}}No evictionNo action{"books": {"43": "Book B"}}
💡 Method completes after evicting cache key 42 and deleting book 42 from database
Variable Tracker
VariableStartAfter Step 1After Step 2
cache.books{"42": "Book A", "43": "Book B"}{"43": "Book B"}{"43": "Book B"}
method.idN/A4242
Key Moments - 2 Insights
Why does the cache entry get removed before the method runs?
Because @CacheEvict removes the cache entry first to avoid stale data if the method changes data, as shown in execution_table step 1.
What happens if the cache key does not exist when @CacheEvict runs?
No error occurs; eviction silently skips missing keys, so cache state remains unchanged except for confirmed evictions (see execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache state after step 1?
A{}
B{"books": {"43": "Book B"}}
C{"books": {"42": "Book A", "43": "Book B"}}
D{"books": {"42": "Book A"}}
💡 Hint
Check the 'Cache State After' column in row for step 1.
At which step does the method delete the book from the database?
AStep 1
BStep 2
CBefore Step 1
DAfter Step 2
💡 Hint
Look at the 'Method Execution' column in execution_table.
If the @CacheEvict annotation was removed, what would happen to the cache after method execution?
ACache would be empty
BCache would remove all entries
CCache would still contain the old entry for key 42
DCache would add a new entry for key 42
💡 Hint
Refer to the cache eviction step in execution_table step 1.
Concept Snapshot
@CacheEvict removes cache entries before method runs.
Use value to specify cache name.
Use key to specify which entry to evict.
Prevents stale data after data changes.
Eviction happens even if method fails.
Full Transcript
When a method annotated with @CacheEvict is called, Spring first removes the specified cache entry. This prevents stale data from remaining if the method changes data. After eviction, the method body executes, such as deleting a record from the database. Finally, the method returns, and the cache no longer contains the evicted entry. If the cache key does not exist, eviction silently skips it without error. This ensures cache consistency with the data source.