-
Notifications
You must be signed in to change notification settings - Fork 1.1k
DOC-4528 async hash examples #3069
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
Merged
tishun
merged 5 commits into
redis:main
from
andy-stark-redis:DOC-4528-async-hash-examples
Dec 6, 2024
+166
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
152ba5b
DOC-4528 first two examples
andy-stark-redis 2cf67ed
DOC-4528 added final example
andy-stark-redis 7d0b392
Merge branch 'main' into DOC-4528-async-hash-examples
andy-stark-redis b74d0c6
DOC-4528 formatting changes
andy-stark-redis 92f338d
DOC-4528 removed unused imports
andy-stark-redis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,166 @@ | ||
| // EXAMPLE: hash_tutorial | ||
| 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.*; | ||
| import java.util.concurrent.CompletableFuture; | ||
| // REMOVE_START | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| // REMOVE_END | ||
|
|
||
| public class HashExample { | ||
|
|
||
| @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("bike:1", "bike:1:stats").toCompletableFuture(); | ||
|
|
||
| // REMOVE_END | ||
|
|
||
| // STEP_START set_get_all | ||
| Map<String, String> bike1 = new HashMap<>(); | ||
| bike1.put("model", "Deimos"); | ||
| bike1.put("brand", "Ergonom"); | ||
| bike1.put("type", "Enduro bikes"); | ||
| bike1.put("price", "4972"); | ||
|
|
||
| CompletableFuture<Void> setGetAll = asyncCommands.hset("bike:1", bike1).thenCompose(res1 -> { | ||
| System.out.println(res1); // >>> 4 | ||
| // REMOVE_START | ||
| assertThat(res1).isEqualTo(4); | ||
| // REMOVE_END | ||
| return asyncCommands.hget("bike:1", "model"); | ||
| }).thenCompose(res2 -> { | ||
| System.out.println(res2); // >>> Deimos | ||
| // REMOVE_START | ||
| assertThat(res2).isEqualTo("Deimos"); | ||
| // REMOVE_END | ||
| return asyncCommands.hget("bike:1", "price"); | ||
| }).thenCompose(res3 -> { | ||
| System.out.println(res3); // >>> 4972 | ||
| // REMOVE_START | ||
| assertThat(res3).isEqualTo("4972"); | ||
| // REMOVE_END | ||
| return asyncCommands.hgetall("bike:1"); | ||
| }) | ||
| // REMOVE_START | ||
| .thenApply(res -> { | ||
| assertThat(res.get("type")).isEqualTo("Enduro bikes"); | ||
| assertThat(res.get("brand")).isEqualTo("Ergonom"); | ||
| assertThat(res.get("price")).isEqualTo("4972"); | ||
| assertThat(res.get("model")).isEqualTo("Deimos"); | ||
|
|
||
| return res; | ||
| }) | ||
| // REMOVE_END | ||
| .thenAccept(System.out::println) | ||
| // >>> {type=Enduro bikes, brand=Ergonom, price=4972, model=Deimos} | ||
| .toCompletableFuture(); | ||
| // STEP_END | ||
|
|
||
| // STEP_START hmget | ||
| CompletableFuture<Void> hmGet = setGetAll.thenCompose(res4 -> { | ||
| return asyncCommands.hmget("bike:1", "model", "price"); | ||
| }) | ||
| // REMOVE_START | ||
| .thenApply(res -> { | ||
| assertThat(res.toString()).isEqualTo("[KeyValue[model, Deimos], KeyValue[price, 4972]]"); | ||
| return res; | ||
| }) | ||
| // REMOVE_END | ||
| .thenAccept(System.out::println) | ||
| // [KeyValue[model, Deimos], KeyValue[price, 4972]] | ||
| .toCompletableFuture(); | ||
| // STEP_END | ||
|
|
||
| // STEP_START hincrby | ||
| CompletableFuture<Void> hIncrBy = hmGet.thenCompose(r -> { | ||
| return asyncCommands.hincrby("bike:1", "price", 100); | ||
| }).thenCompose(res6 -> { | ||
| System.out.println(res6); // >>> 5072 | ||
| // REMOVE_START | ||
| assertThat(res6).isEqualTo(5072L); | ||
| // REMOVE_END | ||
| return asyncCommands.hincrby("bike:1", "price", -100); | ||
| }) | ||
| // REMOVE_START | ||
| .thenApply(res -> { | ||
| assertThat(res).isEqualTo(4972L); | ||
| return res; | ||
| }) | ||
| // REMOVE_END | ||
| .thenAccept(System.out::println) | ||
| // >>> 4972 | ||
| .toCompletableFuture(); | ||
| // STEP_END | ||
|
|
||
| // STEP_START incrby_get_mget | ||
| CompletableFuture<Void> incrByGetMget = asyncCommands.hincrby("bike:1:stats", "rides", 1).thenCompose(res7 -> { | ||
| System.out.println(res7); // >>> 1 | ||
| // REMOVE_START | ||
| assertThat(res7).isEqualTo(1L); | ||
| // REMOVE_END | ||
| return asyncCommands.hincrby("bike:1:stats", "rides", 1); | ||
| }).thenCompose(res8 -> { | ||
| System.out.println(res8); // >>> 2 | ||
| // REMOVE_START | ||
| assertThat(res8).isEqualTo(2L); | ||
| // REMOVE_END | ||
| return asyncCommands.hincrby("bike:1:stats", "rides", 1); | ||
| }).thenCompose(res9 -> { | ||
| System.out.println(res9); // >>> 3 | ||
| // REMOVE_START | ||
| assertThat(res9).isEqualTo(3L); | ||
| // REMOVE_END | ||
| return asyncCommands.hincrby("bike:1:stats", "crashes", 1); | ||
| }).thenCompose(res10 -> { | ||
| System.out.println(res10); // >>> 1 | ||
| // REMOVE_START | ||
| assertThat(res10).isEqualTo(1L); | ||
| // REMOVE_END | ||
| return asyncCommands.hincrby("bike:1:stats", "owners", 1); | ||
| }).thenCompose(res11 -> { | ||
| System.out.println(res11); // >>> 1 | ||
| // REMOVE_START | ||
| assertThat(res11).isEqualTo(1L); | ||
| // REMOVE_END | ||
| return asyncCommands.hget("bike:1:stats", "rides"); | ||
| }).thenCompose(res12 -> { | ||
| System.out.println(res12); // >>> 3 | ||
| // REMOVE_START | ||
| assertThat(res12).isEqualTo("3"); | ||
| // REMOVE_END | ||
| return asyncCommands.hmget("bike:1:stats", "crashes", "owners"); | ||
| }) | ||
| // REMOVE_START | ||
| .thenApply(res -> { | ||
| assertThat(res.toString()).isEqualTo("[KeyValue[crashes, 1], KeyValue[owners, 1]]"); | ||
| return res; | ||
| }) | ||
| // REMOVE_END | ||
| .thenAccept(System.out::println) | ||
| // >>> [KeyValue[crashes, 1], KeyValue[owners, 1]] | ||
| .toCompletableFuture(); | ||
| // STEP_END | ||
|
|
||
| CompletableFuture.allOf( | ||
| // REMOVE_START | ||
| delResult, | ||
| // REMOVE_END, | ||
| hIncrBy, incrByGetMget).join(); | ||
| } finally { | ||
| redisClient.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.