-
Notifications
You must be signed in to change notification settings - Fork 1.1k
DOC-5399 set cmd examples #3342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6e5acaf
b0d4bd7
4196f36
93dc8a5
6c22d0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // EXAMPLE: cmds_set | ||
| package io.redis.examples.async; | ||
|
|
||
| import io.lettuce.core.*; | ||
| import io.lettuce.core.api.async.RedisAsyncCommands; | ||
| import io.lettuce.core.api.StatefulRedisConnection; | ||
|
|
||
| // REMOVE_START | ||
| import org.junit.jupiter.api.Test; | ||
| // REMOVE_END | ||
| import java.util.concurrent.CompletableFuture; | ||
| import static java.util.stream.Collectors.toList; | ||
| // REMOVE_START | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| // REMOVE_END | ||
|
|
||
| public class CmdsSetExample { | ||
|
|
||
| @Test | ||
| public void run() { | ||
| RedisClient redisClient = RedisClient.create("redis://localhost:6379"); | ||
|
|
||
| try (StatefulRedisConnection<String, String> connection = redisClient.connect()) { | ||
| RedisAsyncCommands<String, String> asyncCommands = connection.async(); | ||
| // REMOVE_START | ||
| CompletableFuture<Long> delResult = asyncCommands.del("myset").toCompletableFuture(); | ||
| delResult.join(); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START sadd | ||
| CompletableFuture<Void> sadd = asyncCommands.sadd("myset", "Hello").thenCompose(r -> { | ||
| System.out.println(r); // >>> 1 | ||
| // REMOVE_START | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it sounds dramatic! :-) It just means that this line shouldn't appear in the published example on the docs page (we avoid showing the users the asserts that we test the examples with). |
||
| assertThat(r).isEqualTo(1); | ||
| // REMOVE_END | ||
| return asyncCommands.sadd("myset", "World"); | ||
| }).thenCompose(r -> { | ||
| System.out.println(r); // >>> 1 | ||
| // REMOVE_START | ||
| assertThat(r).isEqualTo(1); | ||
| // REMOVE_END | ||
| return asyncCommands.sadd("myset", "World"); | ||
| }).thenCompose(r -> { | ||
| System.out.println(r); // >>> 0 | ||
| // REMOVE_START | ||
| assertThat(r).isEqualTo(0); | ||
| // REMOVE_END | ||
| return asyncCommands.smembers("myset"); | ||
| }) | ||
| // REMOVE_START | ||
| .thenApply(r -> { | ||
| assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[Hello, World]"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be replaced with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that's a great tip, thanks! The streams thing borrows from what we were doing with the Jedis examples, but your suggestion is much neater :-) |
||
| return r; | ||
| }) | ||
| // REMOVE_END | ||
| .thenAccept(System.out::println) | ||
| // >>> [Hello, World] | ||
| .toCompletableFuture(); | ||
| // STEP_END | ||
| // HIDE_START | ||
| sadd.join(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead joins i would prefer the approach with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We originally did this to avoid the code snippets getting executed out-of-order during testing (some of them use results from previous snippets and so tests fail if a snippet executes before another snippet that it depends on). If this is no longer a problem, or doesn't apply in this case then I'll use TBH, we might have to revisit this stuff carefully now we've got Jupyter notebooks for some of the examples (via the "Run in browser" link). Dependencies between the cells can be a problem with the notebooks too. |
||
| // HIDE_END | ||
| // REMOVE_START | ||
| CompletableFuture<Long> delSaddResult = asyncCommands.del("myset").toCompletableFuture(); | ||
| delSaddResult.join(); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START smembers | ||
| CompletableFuture<Void> smembers = asyncCommands.sadd("myset", "Hello", "World").thenCompose(r -> { | ||
| return asyncCommands.smembers("myset"); | ||
| }) | ||
| // REMOVE_START | ||
| .thenApply(r -> { | ||
| assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[Hello, World]"); | ||
| return r; | ||
| }) | ||
| // REMOVE_END | ||
| .thenAccept(System.out::println) | ||
| // >>> [Hello, World] | ||
| .toCompletableFuture(); | ||
| // STEP_END | ||
| // HIDE_START | ||
| smembers.join(); | ||
| // HIDE_END | ||
| } finally { | ||
| redisClient.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // EXAMPLE: cmds_set | ||
| package io.redis.examples.reactive; | ||
|
|
||
| import io.lettuce.core.*; | ||
| import io.lettuce.core.api.reactive.RedisReactiveCommands; | ||
| import io.lettuce.core.api.StatefulRedisConnection; | ||
|
|
||
| import reactor.core.publisher.Mono; | ||
| // REMOVE_START | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| // REMOVE_END | ||
|
|
||
| public class CmdsSetExample { | ||
|
|
||
| @Test | ||
| public void run() { | ||
| RedisClient redisClient = RedisClient.create("redis://localhost:6379"); | ||
|
|
||
| try (StatefulRedisConnection<String, String> connection = redisClient.connect()) { | ||
| RedisReactiveCommands<String, String> reactiveCommands = connection.reactive(); | ||
| // REMOVE_START | ||
| // Clean up any existing data | ||
| Mono<Void> cleanup = reactiveCommands.del("myset").then(); | ||
| cleanup.block(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like in async we can wrap the block operations in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this was the out-of-order execution during testing again, but as I said for async, I'll certainly change this if it's not a problem here. |
||
| // REMOVE_END | ||
|
|
||
| // STEP_START sadd | ||
| Mono<Void> sadd = reactiveCommands.sadd("myset", "Hello").doOnNext(r -> { | ||
| System.out.println(r); // >>> 1 | ||
| // REMOVE_START | ||
| assertThat(r).isEqualTo(1); | ||
| // REMOVE_END | ||
| }).flatMap(r -> reactiveCommands.sadd("myset", "World")).doOnNext(r -> { | ||
| System.out.println(r); // >>> 1 | ||
| // REMOVE_START | ||
| assertThat(r).isEqualTo(1); | ||
| // REMOVE_END | ||
| }).flatMap(r -> reactiveCommands.sadd("myset", "World")).doOnNext(r -> { | ||
| System.out.println(r); // >>> 0 | ||
| // REMOVE_START | ||
| assertThat(r).isEqualTo(0); | ||
| // REMOVE_END | ||
| }).flatMap(r -> reactiveCommands.smembers("myset").collectList()).doOnNext(r -> { | ||
| System.out.println(r); // >>> [Hello, World] | ||
| // REMOVE_START | ||
| assertThat(r).containsExactlyInAnyOrder("Hello", "World"); | ||
| // REMOVE_END | ||
| }).then(); | ||
| // STEP_END | ||
| // HIDE_START | ||
| sadd.block(); | ||
| // HIDE_END | ||
| // REMOVE_START | ||
| Mono<Long> delSaddResult = reactiveCommands.del("myset"); | ||
| delSaddResult.block(); | ||
| // REMOVE_END | ||
|
|
||
| // STEP_START smembers | ||
| Mono<Void> smembers = reactiveCommands.sadd("myset", "Hello", "World").doOnNext(r -> { | ||
| System.out.println(r); // >>> 2 | ||
| // REMOVE_START | ||
| assertThat(r).isEqualTo(2); | ||
| // REMOVE_END | ||
| }).flatMap(r -> reactiveCommands.smembers("myset").collectList()).doOnNext(r -> { | ||
| System.out.println(r); // >>> [Hello, World] | ||
| // REMOVE_START | ||
| assertThat(r).containsExactlyInAnyOrder("Hello", "World"); | ||
| // REMOVE_END | ||
| }).then(); | ||
| // STEP_END | ||
| // HIDE_START | ||
| smembers.block(); | ||
| // HIDE_END | ||
| } finally { | ||
| redisClient.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RedisClient is
AutoClosableand can be put insidetry()instead callingshutdown()manually.