0
0
Spring Bootframework~10 mins

@Cacheable for read caching in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Cacheable for read caching
Method call with key
Check cache for key
Return cached
Return value
Return value
When a method annotated with @Cacheable is called, Spring checks if the result is in cache. If yes, it returns cached data. If no, it runs the method, caches the result, then returns it.
Execution Sample
Spring Boot
@Cacheable("books")
public Book findBookById(Long id) {
  // simulate slow DB call
  return bookRepository.findById(id).orElse(null);
}
This method caches the book found by id. On first call, it fetches from DB and caches. Next calls with same id return cached book.
Execution Table
StepMethod CallCache CheckCache Hit?ActionReturned Value
1findBookById(1)Check cache for key=1NoCall method, fetch from DBBook{id=1, title='Java Basics'}
2Store result in cache with key=1--Cache updated-
3findBookById(1)Check cache for key=1YesReturn cached valueBook{id=1, title='Java Basics'}
4findBookById(2)Check cache for key=2NoCall method, fetch from DBBook{id=2, title='Spring Guide'}
5Store result in cache with key=2--Cache updated-
6findBookById(2)Check cache for key=2YesReturn cached valueBook{id=2, title='Spring Guide'}
💡 Method calls end when all requested keys are served either from cache or DB.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5Final
cacheemptyempty{1: Book{id=1}}{1: Book{id=1}}{1: Book{id=1}, 2: Book{id=2}}{1: Book{id=1}, 2: Book{id=2}}
Key Moments - 2 Insights
Why does the method not run on the second call with the same id?
Because the cache already has the result for that id (see execution_table step 3), so Spring returns cached data without running the method.
What happens if the cache does not have the key?
The method runs normally to get fresh data, then the result is stored in cache (see steps 1 and 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is returned at step 3?
ACached Book{id=1, title='Java Basics'}
BNew DB call result
CNull
DError
💡 Hint
Check the 'Returned Value' column at step 3 in execution_table.
At which step does the cache first get updated with key=2?
AStep 6
BStep 4
CStep 5
DStep 3
💡 Hint
Look for 'Store result in cache with key=2' in execution_table.
If the cache was empty initially, what would happen at step 1?
ACache returns stale data
BMethod runs and caches result
CMethod returns null
DMethod throws exception
💡 Hint
See execution_table step 1 and 2 for cache miss behavior.
Concept Snapshot
@Cacheable annotation caches method results by key.
On method call, cache is checked first.
If hit, cached value is returned immediately.
If miss, method runs and result is cached.
Improves performance by avoiding repeated DB calls.
Full Transcript
The @Cacheable annotation in Spring Boot helps store method results in a cache. When a method with @Cacheable is called, Spring checks if the result for the given key is already cached. If yes, it returns the cached result without running the method again. If no, it runs the method, stores the result in the cache, and then returns it. This saves time by avoiding repeated slow operations like database calls. For example, calling findBookById(1) first time fetches from DB and caches the book. The next call with id 1 returns the cached book instantly. The execution table shows each step: checking cache, method execution, caching, and returning values. The variable tracker shows how the cache grows as new keys are added. This caching pattern is useful for read-heavy methods where data changes infrequently.