Spring Data Reactive Redis examples
Issue: BAEL-1868
This commit is contained in:
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.spring.data.reactive.redis.template;
|
||||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.connection.ReactiveKeyCommands;
|
||||
import org.springframework.data.redis.connection.ReactiveStringCommands;
|
||||
import org.springframework.data.redis.connection.ReactiveStringCommands.SetCommand;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
public class RedisKeyCommandsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ReactiveKeyCommands keyCommands;
|
||||
|
||||
@Autowired
|
||||
private ReactiveStringCommands stringCommands;
|
||||
|
||||
@Test
|
||||
public void givenFluxOfKeys_whenPerformOperations_thenPerformOperations() {
|
||||
Flux<String> keys = Flux.just("key1", "key2", "key3", "key4");
|
||||
|
||||
Flux<SetCommand> generator = keys.map(String::getBytes)
|
||||
.map(ByteBuffer::wrap)
|
||||
.map(key -> SetCommand.set(key)
|
||||
.value(key));
|
||||
|
||||
StepVerifier.create(stringCommands.set(generator))
|
||||
.expectNextCount(4L)
|
||||
.verifyComplete();
|
||||
|
||||
Mono<Long> keyCount = keyCommands.keys(ByteBuffer.wrap("key*".getBytes()))
|
||||
.flatMapMany(Flux::fromIterable)
|
||||
.count();
|
||||
|
||||
StepVerifier.create(keyCount)
|
||||
.expectNext(4L)
|
||||
.verifyComplete();
|
||||
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.spring.data.reactive.redis.template;
|
||||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.ReactiveListOperations;
|
||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
public class RedisTemplateListOpsIntegrationTest {
|
||||
|
||||
private static final String LIST_NAME = "demo_list";
|
||||
|
||||
@Autowired
|
||||
private ReactiveRedisTemplate<String, String> redisTemplate;
|
||||
|
||||
private ReactiveListOperations<String, String> reactiveListOps;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
reactiveListOps = redisTemplate.opsForList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListAndValues_whenLeftPushAndLeftPop_thenLeftPushAndLeftPop() {
|
||||
Mono<Long> lPush = reactiveListOps.leftPushAll(LIST_NAME, "first", "second")
|
||||
.log("Pushed");
|
||||
|
||||
StepVerifier.create(lPush)
|
||||
.expectNext(2L)
|
||||
.verifyComplete();
|
||||
|
||||
Mono<String> lPop = reactiveListOps.leftPop(LIST_NAME)
|
||||
.log("Popped");
|
||||
|
||||
StepVerifier.create(lPop)
|
||||
.expectNext("second")
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.spring.data.reactive.redis.template;
|
||||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
import com.baeldung.spring.data.reactive.redis.model.Employee;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
||||
import org.springframework.data.redis.core.ReactiveValueOperations;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
public class RedisTemplateValueOpsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ReactiveRedisTemplate<String, Employee> redisTemplate;
|
||||
|
||||
private ReactiveValueOperations<String, Employee> reactiveValueOps;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
reactiveValueOps = redisTemplate.opsForValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployee_whenSet_thenSet() {
|
||||
|
||||
Mono<Boolean> result = reactiveValueOps.set("123", new Employee("123", "Bill", "Accounts"));
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(true)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeId_whenGet_thenReturnsEmployee() {
|
||||
|
||||
Mono<Employee> fetchedEmployee = reactiveValueOps.get("123");
|
||||
|
||||
StepVerifier.create(fetchedEmployee)
|
||||
.expectNext(new Employee("123", "Bill", "Accounts"))
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployee_whenSetWithExpiry_thenSetsWithExpiryTime() throws InterruptedException {
|
||||
|
||||
Mono<Boolean> result = reactiveValueOps.set("129", new Employee("129", "John", "Programming"), Duration.ofSeconds(1));
|
||||
|
||||
Mono<Employee> fetchedEmployee = reactiveValueOps.get("129");
|
||||
|
||||
StepVerifier.create(result)
|
||||
.expectNext(true)
|
||||
.verifyComplete();
|
||||
|
||||
Thread.sleep(2000L);
|
||||
|
||||
StepVerifier.create(fetchedEmployee)
|
||||
.expectNextCount(0L)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -31,7 +31,7 @@ public class RedisMessageListenerIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new redis.embedded.RedisServer(6379);
|
||||
redisServer = new redis.embedded.RedisServer(6380);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ public class StudentRepositoryIntegrationTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new redis.embedded.RedisServer(6379);
|
||||
redisServer = new redis.embedded.RedisServer(6380);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user