0
0
Spring Bootframework~10 mins

@EnableCaching annotation in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @EnableCaching annotation
Start Application
Spring Boot scans for @EnableCaching
Enable Cache Infrastructure
Detect @Cacheable, @CachePut, @CacheEvict
Wrap methods with caching logic
On method call: Check cache
Return cached
Store result in cache
Return result
End
This flow shows how Spring Boot detects @EnableCaching, sets up caching, and intercepts method calls to use or update the cache.
Execution Sample
Spring Boot
@SpringBootApplication
@EnableCaching
public class App {
  @Cacheable("books")
  public Book getBook(String id) {
    // fetch book from DB
  }
}
This code enables caching and caches results of getBook method by book id.
Execution Table
StepActionCache StateMethod CallCache Hit?Result Returned
1Application starts, @EnableCaching detectedEmptyNoN/AN/A
2Call getBook("123")EmptyYesNoFetch from DB
3Store result in cache key "123"Contains key "123"NoN/AN/A
4Call getBook("123") againContains key "123"YesYesReturn cached book
5Call getBook("456")Contains keys "123"YesNoFetch from DB
6Store result in cache key "456"Contains keys "123", "456"NoN/AN/A
7Call getBook("123") againContains keys "123", "456"YesYesReturn cached book
💡 Execution stops when no more method calls occur.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
cache{}{"123": Book123}{"123": Book123}{"123": Book123}{"123": Book123, "456": Book456}{"123": Book123, "456": Book456}
Key Moments - 2 Insights
Why does the method execute the first time but not the second time for the same argument?
At step 2, the cache is empty so the method runs and result is stored (step 3). At step 4, the cache has the key so the cached result is returned without running the method.
What happens if a new argument is passed to the cached method?
At step 5, the argument "456" is not in cache, so the method runs and stores the result (step 6). This shows caching is per argument.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state after step 3?
AContains key "123"
BEmpty
CContains keys "123" and "456"
DContains key "456" only
💡 Hint
Check the 'Cache State' column at step 3 in the execution table.
At which step does the method getBook("123") return a cached result?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'Cache Hit?' column for getBook("123") calls in the execution table.
If @EnableCaching was removed, how would the cache state change after step 3?
ACache would still contain key "123"
BCache would remain empty
CCache would contain keys "123" and "456"
DCache would contain key "456" only
💡 Hint
Without @EnableCaching, caching is not activated, so cache stays empty.
Concept Snapshot
@EnableCaching enables Spring's caching support.
Add @EnableCaching on a config class to activate caching.
Use @Cacheable on methods to cache results by arguments.
On method call, cache is checked first.
If cached, return cached result; else run method and cache result.
Caching improves performance by avoiding repeated work.
Full Transcript
The @EnableCaching annotation tells Spring Boot to turn on caching features. When the app starts, Spring scans for this annotation and sets up caching behind the scenes. Methods marked with @Cacheable will have their results saved in a cache after the first call. Later calls with the same arguments return the cached result instead of running the method again. This saves time and resources. The execution table shows how the cache starts empty, fills with results after method calls, and returns cached data on repeated calls. Without @EnableCaching, caching does not happen and the cache stays empty. This simple setup helps apps run faster by reusing previous results.