π Database Caching In Spring Boot?
Database Caching stores frequently accessed data in memory (RAM), so your app doesnβt hit the database every time.
π― Result: Faster performance + reduced DB load
β When to Use It?
- Same data is read again and again (e.g., user info, product list)
- Performance is critical (e.g., high-traffic APIs)
π οΈ How to Enable Caching in Spring Boot
β Step 1: Add Starter Dependency
In pom.xml
:
β Step 2: Enable Caching in Main Class
β
Step 3: Use @Cacheable
in Your Service
π Now, the first call will fetch from the DB and store it in cache.
Next time? Fetched from memory β faster!
βοΈ How It Works
-
@Cacheable
β stores method result -
@CacheEvict
β removes entry from cache -
@CachePut
β updates entry in cache
π§ What Cache is Used?
By default: Simple in-memory (ConcurrentMap)
For production: Use EhCache, Redis, or Caffeine