life is too short for a diary




An Introduction to Spring Data Redis Reactive

Tags: redis java

Author
Written by: Tushar Sharma
Featured image for An Introduction to Spring Data Redis Reactive

Spring Data Redis Reactive offers a powerful solution for working with Redis in a non-blocking, reactive manner. Let's dive into the key components and how to use them effectively.

Setting Up Dependencies

To get started with Spring Data Redis Reactive, you need to include the appropriate dependency in your project. If you're using Maven, add the following to your pom.xml:

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

For Gradle users, add this to your build.gradle:

implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'

This starter dependency brings in everything you need for reactive Redis operations, including the Lettuce driver, which is the preferred client for reactive Redis usage in Spring.

Spring Data Redis Reactive provides three main components for interacting with Redis:

ReactiveRedisConnectionFactory

ReactiveRedisConnectionFactory is the cornerstone of Redis interactions in a reactive Spring application. It's responsible for creating and managing reactive Redis connections.

Key Features

When to Use

Use ReactiveRedisConnectionFactory when you need:

Code Example

@Service public class LowLevelRedisService { private final ReactiveRedisConnectionFactory connectionFactory;

public LowLevelRedisService(ReactiveRedisConnectionFactory connectionFactory) {
    this.connectionFactory = connectionFactory;
}

public Mono<String> executeCustomCommand(String key) {
    return connectionFactory.getReactiveConnection()
        .stringCommands()
        .get(ByteBuffer.wrap(key.getBytes()))
        .map(ByteBuffer::array)
        .map(String::new);
} }

ReactiveRedisOperations

ReactiveRedisOperations is an interface that defines a set of reactive Redis operations. It provides a higher-level abstraction compared to ReactiveRedisConnectionFactory.

Key Features

When to Use

Use ReactiveRedisOperations when you:

Code Example

@Service public class UserService { private final ReactiveRedisOperations<String, User> redisOperations;

public UserService(ReactiveRedisOperations<String, User> redisOperations) {
    this.redisOperations = redisOperations;
}

public Mono<User> saveUser(User user) {
    return redisOperations.opsForValue().set(user.getId(), user)
        .thenReturn(user);
}

public Mono<User> getUser(String id) {
    return redisOperations.opsForValue().get(id);
} }

ReactiveRedisTemplate

Key Features

Implements ReactiveRedisOperations interface

When to Use

Use ReactiveRedisTemplate when you:

Code Example

@Service public class CacheService { private final ReactiveRedisTemplate<String, User> redisTemplate;

public CacheService(ReactiveRedisTemplate<String, User> redisTemplate) {
    this.redisTemplate = redisTemplate;
}

public Mono<User> cacheUser(User user) {
    return redisTemplate.opsForValue().set(user.getId(), user, Duration.ofMinutes(30))
        .thenReturn(user);
}

public Mono<User> getCachedUser(String id) {
    return redisTemplate.opsForValue().get(id);
} }

All these Beans are autoconfigured by spring based on the dependency.


comments powered by Disqus