Redis helps your Spring Boot app remember data quickly. It stores data in memory so your app can get it fast without waiting for slow databases.
0
0
Redis as cache provider in Spring Boot
Introduction
You want to speed up repeated data requests in your app.
You need to reduce load on your main database by caching results.
You want to store session data or user preferences temporarily.
You want to share cached data between multiple app instances.
You want a simple way to improve app performance with minimal code changes.
Syntax
Spring Boot
spring.cache.type=redis spring.redis.host=localhost spring.redis.port=6379 @EnableCaching @Configuration public class CacheConfig { @Bean public RedisCacheManager cacheManager(RedisConnectionFactory factory) { return RedisCacheManager.builder(factory).build(); } }
Set Redis connection details in application.properties or application.yml.
Use @EnableCaching to activate caching in Spring Boot.
Examples
This caches the result of
getBookById in Redis under the 'books' cache.Spring Boot
@Cacheable("books")
public Book getBookById(Long id) {
// method logic
}This removes a specific book from the Redis cache when called.
Spring Boot
@CacheEvict(value = "books", key = "#id") public void removeBookFromCache(Long id) { // method logic }
This updates the cached book data after modifying it.
Spring Boot
@CachePut(value = "books", key = "#book.id") public Book updateBook(Book book) { // method logic return book; }
Sample Program
This Spring Boot app uses Redis to cache book titles. The first call to getBookTitleById waits 3 seconds, but later calls return instantly from cache.
Spring Boot
package com.example.cache; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.stereotype.Service; @SpringBootApplication @EnableCaching public class CacheApplication { public static void main(String[] args) { SpringApplication.run(CacheApplication.class, args); } @Configuration static class CacheConfig { @Bean public RedisCacheManager cacheManager(RedisConnectionFactory factory) { return RedisCacheManager.builder(factory).build(); } } @Bean public CommandLineRunner demo(BookService bookService) { return args -> { System.out.println("Calling getBookTitleById(1) first time..."); String result1 = bookService.getBookTitleById(1L); System.out.println("Result: " + result1); System.out.println("Calling getBookTitleById(1) second time..."); String result2 = bookService.getBookTitleById(1L); System.out.println("Result: " + result2); }; } @Service static class BookService { @Cacheable("books") public String getBookTitleById(Long id) { simulateSlowService(); return "Book Title " + id; } private void simulateSlowService() { try { Thread.sleep(3000); // simulate delay } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }
OutputSuccess
Important Notes
Make sure Redis server is running locally or update host/port accordingly.
Cached data stays in Redis until expired or evicted.
Use @Cacheable on methods that return data you want to cache.
Summary
Redis stores data in memory for fast access in Spring Boot apps.
Use @EnableCaching and RedisCacheManager to enable Redis caching.
Annotate methods with @Cacheable to cache their results automatically.