Using Redis Cache with Spring Boot

In this article, we will see how to integrate Redis Cache with Spring Boot. We will learn how we can configure Redis data inside the Spring boot cache.

Let's look at the dependencies first that are required to import Redis into a Spring boot application.


// Adding spring-boot cache & redis dependencies


After adding the Redis dependencies, you now need to perform some configuration so that it could be used in your project. Spring Boot will automatically configure a Redis-cache Manager but with default properties. We can modify this configuration and change it as per our requirement.

public RedisCacheConfiguration cacheConfiguration() {
   return RedisCacheConfiguration.defaultCacheConfig()
   .entryTtl(Duration.ofMinutes(60)) // Changing the cache TTL
   //Defining serialization to retrieve data

Modifying the configurations gives us more control over the basic cache configuration. For example, we can change the default time-to-live values and customize the default serialization strategy to in-flight cache creation, as shown above.


Let's take an example that we have an EmployeeService component that retrieves employee information from the database. Since we need to get the employee details for multiple things, we need this data to be fast which is why we are using Redis cache. Let's create an integration test for the same

@Import({ CacheConfig.class, EmployeeService.class})
@ImportAutoConfiguration(classes = {
class EmployeeServiceCachingIntegrationTest {

   private EmployeeRepository mockEmployeeRepository;

   private EmployeeService employeeService;

   private CacheManager cacheManager;

   givenRedisCaching_whenFindEmployeeById_thenEmployeeReturnedFromCache() {
      Employee employee = new Employee(id, A_DESCRIPTION);

      Employee employeeNotFromCache = employeeService.getEmployeeById(id);
      Employee employeeFromCache = employeeService.getEmployeeById(id);


      verify(mockEmployeeRepository, times(1)).findById(id);

Retrieving data from Cache

@Cacheable(value = "employeeCache")
public Employee getEmployeeForId(String id) {
   return employeeRepository.findById(id)

We have invoked the getEmployeeById() twice. The first invocation should obtain the item from the repository, whereas the second invocation should return the same employee object from the cache without invoking the repository.