Skip to content

Commit f9d4509

Browse files
DOC-4528 async hash examples (#3069)
* DOC-4528 first two examples * DOC-4528 added final example * DOC-4528 formatting changes * DOC-4528 removed unused imports
1 parent 28a4154 commit f9d4509

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// EXAMPLE: hash_tutorial
2+
package io.redis.examples.async;
3+
4+
import io.lettuce.core.*;
5+
import io.lettuce.core.api.async.RedisAsyncCommands;
6+
import io.lettuce.core.api.StatefulRedisConnection;
7+
8+
// REMOVE_START
9+
import org.junit.jupiter.api.Test;
10+
// REMOVE_END
11+
import java.util.*;
12+
import java.util.concurrent.CompletableFuture;
13+
// REMOVE_START
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
// REMOVE_END
16+
17+
public class HashExample {
18+
19+
@Test
20+
public void run() {
21+
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
22+
23+
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
24+
RedisAsyncCommands<String, String> asyncCommands = connection.async();
25+
// REMOVE_START
26+
CompletableFuture<Long> delResult = asyncCommands.del("bike:1", "bike:1:stats").toCompletableFuture();
27+
28+
// REMOVE_END
29+
30+
// STEP_START set_get_all
31+
Map<String, String> bike1 = new HashMap<>();
32+
bike1.put("model", "Deimos");
33+
bike1.put("brand", "Ergonom");
34+
bike1.put("type", "Enduro bikes");
35+
bike1.put("price", "4972");
36+
37+
CompletableFuture<Void> setGetAll = asyncCommands.hset("bike:1", bike1).thenCompose(res1 -> {
38+
System.out.println(res1); // >>> 4
39+
// REMOVE_START
40+
assertThat(res1).isEqualTo(4);
41+
// REMOVE_END
42+
return asyncCommands.hget("bike:1", "model");
43+
}).thenCompose(res2 -> {
44+
System.out.println(res2); // >>> Deimos
45+
// REMOVE_START
46+
assertThat(res2).isEqualTo("Deimos");
47+
// REMOVE_END
48+
return asyncCommands.hget("bike:1", "price");
49+
}).thenCompose(res3 -> {
50+
System.out.println(res3); // >>> 4972
51+
// REMOVE_START
52+
assertThat(res3).isEqualTo("4972");
53+
// REMOVE_END
54+
return asyncCommands.hgetall("bike:1");
55+
})
56+
// REMOVE_START
57+
.thenApply(res -> {
58+
assertThat(res.get("type")).isEqualTo("Enduro bikes");
59+
assertThat(res.get("brand")).isEqualTo("Ergonom");
60+
assertThat(res.get("price")).isEqualTo("4972");
61+
assertThat(res.get("model")).isEqualTo("Deimos");
62+
63+
return res;
64+
})
65+
// REMOVE_END
66+
.thenAccept(System.out::println)
67+
// >>> {type=Enduro bikes, brand=Ergonom, price=4972, model=Deimos}
68+
.toCompletableFuture();
69+
// STEP_END
70+
71+
// STEP_START hmget
72+
CompletableFuture<Void> hmGet = setGetAll.thenCompose(res4 -> {
73+
return asyncCommands.hmget("bike:1", "model", "price");
74+
})
75+
// REMOVE_START
76+
.thenApply(res -> {
77+
assertThat(res.toString()).isEqualTo("[KeyValue[model, Deimos], KeyValue[price, 4972]]");
78+
return res;
79+
})
80+
// REMOVE_END
81+
.thenAccept(System.out::println)
82+
// [KeyValue[model, Deimos], KeyValue[price, 4972]]
83+
.toCompletableFuture();
84+
// STEP_END
85+
86+
// STEP_START hincrby
87+
CompletableFuture<Void> hIncrBy = hmGet.thenCompose(r -> {
88+
return asyncCommands.hincrby("bike:1", "price", 100);
89+
}).thenCompose(res6 -> {
90+
System.out.println(res6); // >>> 5072
91+
// REMOVE_START
92+
assertThat(res6).isEqualTo(5072L);
93+
// REMOVE_END
94+
return asyncCommands.hincrby("bike:1", "price", -100);
95+
})
96+
// REMOVE_START
97+
.thenApply(res -> {
98+
assertThat(res).isEqualTo(4972L);
99+
return res;
100+
})
101+
// REMOVE_END
102+
.thenAccept(System.out::println)
103+
// >>> 4972
104+
.toCompletableFuture();
105+
// STEP_END
106+
107+
// STEP_START incrby_get_mget
108+
CompletableFuture<Void> incrByGetMget = asyncCommands.hincrby("bike:1:stats", "rides", 1).thenCompose(res7 -> {
109+
System.out.println(res7); // >>> 1
110+
// REMOVE_START
111+
assertThat(res7).isEqualTo(1L);
112+
// REMOVE_END
113+
return asyncCommands.hincrby("bike:1:stats", "rides", 1);
114+
}).thenCompose(res8 -> {
115+
System.out.println(res8); // >>> 2
116+
// REMOVE_START
117+
assertThat(res8).isEqualTo(2L);
118+
// REMOVE_END
119+
return asyncCommands.hincrby("bike:1:stats", "rides", 1);
120+
}).thenCompose(res9 -> {
121+
System.out.println(res9); // >>> 3
122+
// REMOVE_START
123+
assertThat(res9).isEqualTo(3L);
124+
// REMOVE_END
125+
return asyncCommands.hincrby("bike:1:stats", "crashes", 1);
126+
}).thenCompose(res10 -> {
127+
System.out.println(res10); // >>> 1
128+
// REMOVE_START
129+
assertThat(res10).isEqualTo(1L);
130+
// REMOVE_END
131+
return asyncCommands.hincrby("bike:1:stats", "owners", 1);
132+
}).thenCompose(res11 -> {
133+
System.out.println(res11); // >>> 1
134+
// REMOVE_START
135+
assertThat(res11).isEqualTo(1L);
136+
// REMOVE_END
137+
return asyncCommands.hget("bike:1:stats", "rides");
138+
}).thenCompose(res12 -> {
139+
System.out.println(res12); // >>> 3
140+
// REMOVE_START
141+
assertThat(res12).isEqualTo("3");
142+
// REMOVE_END
143+
return asyncCommands.hmget("bike:1:stats", "crashes", "owners");
144+
})
145+
// REMOVE_START
146+
.thenApply(res -> {
147+
assertThat(res.toString()).isEqualTo("[KeyValue[crashes, 1], KeyValue[owners, 1]]");
148+
return res;
149+
})
150+
// REMOVE_END
151+
.thenAccept(System.out::println)
152+
// >>> [KeyValue[crashes, 1], KeyValue[owners, 1]]
153+
.toCompletableFuture();
154+
// STEP_END
155+
156+
CompletableFuture.allOf(
157+
// REMOVE_START
158+
delResult,
159+
// REMOVE_END,
160+
hIncrBy, incrByGetMget).join();
161+
} finally {
162+
redisClient.shutdown();
163+
}
164+
}
165+
166+
}

0 commit comments

Comments
 (0)