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