forked from spring-projects/spring-data-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-projects#323 - Add examples for Reactive Redis Template.
- Loading branch information
Showing
5 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
redis/reactive/src/test/java/example/springdata/redis/operations/ListOperationsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.springdata.redis.operations; | ||
|
||
import example.springdata.redis.RedisTestConfiguration; | ||
import example.springdata.redis.test.util.RequiresRedisServer; | ||
import lombok.extern.slf4j.Slf4j; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.time.Duration; | ||
import java.util.logging.Level; | ||
|
||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
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.ReactiveRedisOperations; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
/** | ||
* Show usage of reactive Template API on Redis lists using {@link ReactiveRedisOperations}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = RedisTestConfiguration.class) | ||
public class ListOperationsTests { | ||
|
||
// we only want to run this tests when redis is up an running | ||
public static @ClassRule RequiresRedisServer requiresServer = RequiresRedisServer.onLocalhost(); | ||
|
||
@Autowired ReactiveRedisOperations<String, String> operations; | ||
|
||
@Before | ||
public void before() { | ||
StepVerifier.create(operations.execute(it -> it.serverCommands().flushDb())).expectNext("OK").verifyComplete(); | ||
} | ||
|
||
/** | ||
* A simple queue using Redis blocking list commands {@code BLPOP} and {@code LPUSH} to produce the queue message. | ||
*/ | ||
@Test | ||
public void shouldPollAndPopulateQueue() { | ||
|
||
String queue = "foo"; | ||
|
||
ReactiveListOperations<String, String> listOperations = operations.opsForList(); | ||
|
||
Mono<String> blpop = listOperations // | ||
.leftPop(queue, Duration.ofSeconds(30)) // | ||
.log("example.springdata.redis", Level.INFO); | ||
|
||
log.info("Blocking pop...waiting for message"); | ||
StepVerifier.create(blpop) // | ||
.then(() -> { | ||
|
||
Mono.delay(Duration.ofSeconds(10)).doOnSuccess(it -> { | ||
|
||
log.info("Subscriber produces message"); | ||
|
||
}).then(listOperations.leftPush(queue, "Hello, World!")).subscribe(); | ||
|
||
}).expectNext("Hello, World!").verifyComplete(); | ||
|
||
log.info("Blocking pop...done!"); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
redis/reactive/src/test/java/example/springdata/redis/operations/ValueOperationsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package example.springdata.redis.operations; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import example.springdata.redis.RedisTestConfiguration; | ||
import example.springdata.redis.test.util.RequiresRedisServer; | ||
import lombok.extern.slf4j.Slf4j; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.time.Duration; | ||
|
||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
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.ReactiveRedisOperations; | ||
import org.springframework.data.redis.core.ReactiveValueOperations; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
/** | ||
* Show usage of reactive Template API on Redis strings using {@link ReactiveRedisOperations}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = RedisTestConfiguration.class) | ||
public class ValueOperationsTests { | ||
|
||
// we only want to run this tests when redis is up an running | ||
public static @ClassRule RequiresRedisServer requiresServer = RequiresRedisServer.onLocalhost(); | ||
|
||
@Autowired ReactiveRedisOperations<String, String> operations; | ||
|
||
@Before | ||
public void before() { | ||
StepVerifier.create(operations.execute(it -> it.serverCommands().flushDb())).expectNext("OK").verifyComplete(); | ||
} | ||
|
||
/** | ||
* Implement a simple caching sequence using {@code GET} and {@code SETEX} commands. | ||
*/ | ||
@Test | ||
public void shouldCacheValue() { | ||
|
||
String cacheKey = "foo"; | ||
|
||
ReactiveValueOperations<String, String> valueOperations = operations.opsForValue(); | ||
|
||
Mono<String> cachedMono = valueOperations.get(cacheKey) // | ||
.switchIfEmpty(cacheValue().flatMap(it -> { | ||
|
||
return valueOperations.set(cacheKey, it, Duration.ofSeconds(60)).then(Mono.just(it)); | ||
})); | ||
|
||
log.info("Initial access (takes a while...)"); | ||
|
||
StepVerifier.create(cachedMono).expectSubscription() // | ||
.expectNoEvent(Duration.ofSeconds(9)) // | ||
.expectNext("Hello, World!") // | ||
.verifyComplete(); | ||
|
||
log.info("Subsequent access (use cached value)"); | ||
|
||
Duration duration = StepVerifier.create(cachedMono) // | ||
.expectNext("Hello, World!") // | ||
.verifyComplete(); | ||
|
||
log.info("Done"); | ||
|
||
assertThat(duration).isLessThan(Duration.ofSeconds(2)); | ||
} | ||
|
||
/** | ||
* @return the cache value that is expensive to calculate. | ||
*/ | ||
private Mono<String> cacheValue() { | ||
return Mono.delay(Duration.ofSeconds(10)).then(Mono.just("Hello, World!")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
logging.level.root=WARN | ||
logging.level.example.springdata.redis=INFO |