Spring Boot Add Redis Caching

Farhan Tanvir
Code With Farhan
Published in
1 min readJan 8, 2024

This tutorial will guide you to use redis caching in a spring boot application.

Add the following two dependencies in pom.xml file :

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

In the application.properties file add the following configurations :

spring.data.redis.host=<redis_host_name>
spring.data.redis.port=14541
spring.data.redis.password=<redis_password>

Use your own redis host name and password.

Adding Cache Configuration

Create a package named config and add the following class CacheConfig.java :

package com.example.springmvcdemo.config;

import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;

@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
return (builder) -> builder
.withCacheConfiguration("categories",
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(60))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())));
}
}

We have added a redis key “categories” with 60 minutes TTL to store json value. So we have used GenericJacson2JsonRedisSerializer. We can add multiple redis key with different TTL with withCacheConfiguration() method. For example :

@Bean
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
return (builder) -> builder
.withCacheConfiguration("key1",
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(60))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())))
.withCacheConfiguration("key2",
RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())));
}

Now add the “@Cachable” annotation with the method you want to cache.

@Service
public class CategoryService {

private final CategoryRepository categoryRepository;

@Cacheable("categories")
public List<Category> getAllCategories(){
return categoryRepository.findAll();
}
}

The “@Cacheable” annotation will fetch a list of categories from redis and cache it in redis with “categories” key for 60 minutes TTL.

--

--