Redis is a high-performance, in-memory key-value store often used for caching in Java applications. When integrated with Spring Boot, it becomes very easy to use with annotations like
✅ 1. Add Maven Dependency (
✅ 3. Configure Redis in
Entity (e.g.,
✅ 5. Cache Logic with
@Cacheable, @CachePut, and @CacheEvict.🔧 Step-by-Step: Using Redis Cache in Java (Spring Boot)
✅ 1. Add Maven Dependency (spring-boot-starter-data-redis)
You may also need:
And for embedded Redis during testing (optional):
✅ 2. Enable Caching in Main Class
✅ 3. Configure Redis in application.properties
✅ 4. Example Entity and Repository
Entity (e.g., Department.java):
Repository:
✅ 5. Cache Logic with @Cacheable
✅ 6. Redis Console
Make sure Redis is running:
You can view keys via:
✅ Sample Behavior
-
First call to
getDepartmentById(1)→ takes 3 seconds (simulated) -
Next call → instant (from Redis cache)
✅ Summary
| Annotation | Purpose |
|---|---|
@Cacheable | Caches the result of a method call |
@CachePut | Updates the cache after method call |
@CacheEvict | Removes entry from cache |
@EnableCaching | Enables Spring’s cache abstraction |
https://www.welookups.com/cssref/pr_border-style.html
ReplyDelete