Spring Boot - Spring Data Redis

Lets learn to integrate Spring Data Redis into a Spring Boot application.

First, you need a Redis server. You can start a redis server as Docker container:

1
docker run -d --rm -p 6379:6379 --name redis-demo redis

Maven Dependency

You need to include spring-boot-starter-data-redis dependency in your spring boot’s pom.xml file in order to use Spring Data Redis.

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Configuration

Here is an example of Redis Configuration. The configuration below sets up a redis connection to localhost:6379 and RedisTemplate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Bean
public LettuceConnectionFactory jedisConnectionFactory() {
return new LettuceConnectionFactory("localhost", 6379);
}

@Bean
RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
}

We need to provide @EnableRedisRepositories annotation to tell Spring boot to use Reids Repositories.

LettuceConnectionFactory is a connection factory that creates Lettuce-based connections. Here we configure by providing hostname and port number. In reality, it should be configured using environmetnal configuration.

RedisTemplate is used for Redis data access.

Another approach is to configure the Redis connection in application.yml. Spring will automatically configure RedisConnectionFactory and RedisTemplate for you.

1
2
3
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=

Entity

A very simple object that annotated with @RedisHash. The identity should have @Id annotation.

1
2
3
4
5
6
7
8
9
10
11
12
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

@Data
@RedisHash("customer")
public class Customer {
@Id
private Long id;

private String name;
}

Repository

Redis Repository class are interfaces that extends CrudRepository interface.

1
2
3
4
import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> {
}

Controller

Controller class that tests the repository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@RestController
@RequestMapping("/customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;

// GET localhost:8080/customers/all
@GetMapping("/all")
public Iterable<Customer> all() {
return customerRepository.findAll();
}

// POST localhost:8080/cusotmers/save
// curl -X POST -H 'Content-Type: application/json' localhost:8080/customers/save -d '{"name": "James"}'
@PostMapping("/save")
public Customer save(@RequestBody Customer customer) {
customerRepository.save(customer);
return customer;
}

// DELETE localhost:8080/cusotmers/{id}
// curl -X DELETE localhost:8080/customers/-2768644627155708900
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
customerRepository.deleteById(id);
}
}

Execute POST http request to save a customer to redis data store.

1
curl -X POST -H 'Content-Type: application/json' localhost:8080/customers/save -d '{"name": "James"}'

go to localhost:8080/customers/all to view all the customers in the redis data store.

source code for this post

Reference