Redis is an open source, in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries.
In this article, we will show you how to use Spring Boot with Redis.
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, and geospatial indexes with radius queries.
Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
Spring Boot provides first-class support for Redis. In this section, we will show you how to use Spring Boot with Redis.
We will use the lettuce library for communicating with Redis. Lettuce is a fully non-blocking Redis client based on Netty for handling Redis connections and Spring Data for object mapping.
First, we need to add the following dependencies to our pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
By default, Spring Boot will look for a Redis server at localhost:6379
. We can change this by setting the spring.redis.host
and spring.redis.port
properties in our application.properties file:
spring.redis.host=localhost
spring.redis.port=6379
We can also specify a password for Redis if it is password protected:
spring.redis.password=secret
Spring Data Redis is the Redis specific implementation of Spring Data. It provides the abstractions of the Jedis and Lettuce Redis clients and provides a unified object-mapper interface for Redis.
RedisTemplate is the central class in Spring Data Redis. It defines high-level Redis operations against data types used in Spring applications.
We can configure a RedisTemplate in our Spring application context:
@Bean
public RedisTemplate<String, Object> redisTemplate(
RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
return template;
}
StringRedisTemplate is a specialized RedisTemplate that is meant for handling String-based data in Redis.
We can configure a StringRedisTemplate in our Spring application context:
@Bean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(factory);
return template;
}
Spring Data Redis provides a Redis specific repository abstraction. We can use this abstraction to create Redis repositories.
First, we need to create a Redis specific interface that extends the Repository interface:
public interface PersonRepository extends Repository<Person, String> {
Person findById(String id);
List<Person> findAll();
Person save(Person person);
void delete(Person person);
}
Next, we need to create an implementation of our PersonRepository interface:
@Service
public class PersonRepositoryImpl implements PersonRepository {
private final RedisTemplate<String, Person> redisTemplate;
@Autowired
public PersonRepositoryImpl(RedisTemplate<String, Person> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@Override
public Person findById(String id) {
return redisTemplate.opsForValue().get(id);
}
@Override
public List<Person> findAll() {
return redisTemplate.opsForValue().multiGet(redisTemplate.keys("*"));
}
@Override
public Person save(Person person) {
redisTemplate.opsForValue().set(person.getId(), person);
return person;
}
@Override
public void delete(Person person) {
redisTemplate.delete(person.getId());
}
}
In the above example, we are using the RedisTemplate to interact with Redis.
Spring Data Redis uses Jackson for serializing and deserializing objects to and from JSON. By default, Jackson is configured to use the JAXB annotation introspector.
We can configure Jackson to use the Jackson annotation introspector instead of the JAXB annotation introspector by setting the spring.jackson.deserialization.USE_JACKSON_ANNOTATIONS
property to true
in our application.properties file:
spring.jackson.deserialization.USE_JACKSON_ANNOTATIONS=true
We can also configure Jackson to use the Jackson annotation introspector by using a custom ObjectMapper:
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.USE_JACKSON_ANNOTATIONS, true);
return mapper;
}
In this article, we have shown you how to use Spring Boot with Redis.