|
| 1 | +// EXAMPLE: ss_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 SortedSetExample { |
| 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 | + asyncCommands.del("racer_scores").toCompletableFuture().join(); |
| 27 | + // REMOVE_END |
| 28 | + |
| 29 | + // STEP_START zadd |
| 30 | + CompletableFuture<Void> zadd = asyncCommands.zadd("racer_scores", ScoredValue.just(10d, "Norem")) |
| 31 | + .thenCompose(res1 -> { |
| 32 | + System.out.println(res1); // >>> 1 |
| 33 | + // REMOVE_START |
| 34 | + assertThat(res1).isEqualTo(1); |
| 35 | + // REMOVE_END |
| 36 | + |
| 37 | + return asyncCommands.zadd("racer_scores", ScoredValue.just(12d, "Castilla")); |
| 38 | + }).thenCompose(res2 -> { |
| 39 | + System.out.println(res2); // >>> 1 |
| 40 | + // REMOVE_START |
| 41 | + assertThat(res2).isEqualTo(1); |
| 42 | + // REMOVE_END |
| 43 | + |
| 44 | + return asyncCommands.zadd("racer_scores", ScoredValue.just(8d, "Sam-Bodden"), |
| 45 | + ScoredValue.just(10d, "Royce"), ScoredValue.just(6d, "Ford"), |
| 46 | + ScoredValue.just(14d, "Prickett")); |
| 47 | + }) |
| 48 | + // REMOVE_START |
| 49 | + .thenApply(res -> { |
| 50 | + assertThat(res).isEqualTo(4); |
| 51 | + return res; |
| 52 | + }) |
| 53 | + // REMOVE_END |
| 54 | + .thenAccept(System.out::println) // >>> 4 |
| 55 | + .toCompletableFuture(); |
| 56 | + // STEP_END |
| 57 | + zadd.join(); |
| 58 | + |
| 59 | + // STEP_START zrange |
| 60 | + CompletableFuture<Void> zrange = asyncCommands.zrange("racer_scores", 0, -1).thenCompose(res3 -> { |
| 61 | + System.out.println(res3); |
| 62 | + // >>> [Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett] |
| 63 | + // REMOVE_START |
| 64 | + assertThat(res3.toString()).isEqualTo("[Ford, Sam-Bodden, Norem, Royce, Castilla, Prickett]"); |
| 65 | + // REMOVE_END |
| 66 | + |
| 67 | + return asyncCommands.zrevrange("racer_scores", 0, -1); |
| 68 | + }) |
| 69 | + // REMOVE_START |
| 70 | + .thenApply((List<String> res) -> { |
| 71 | + assertThat(res.toString()).isEqualTo("[Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford]"); |
| 72 | + return res; |
| 73 | + }) |
| 74 | + // REMOVE_END |
| 75 | + .thenAccept(System.out::println) |
| 76 | + // >>> [Prickett, Castilla, Royce, Norem, Sam-Bodden, Ford] |
| 77 | + .toCompletableFuture(); |
| 78 | + // STEP_END |
| 79 | + zrange.join(); |
| 80 | + |
| 81 | + // STEP_START zrange_withscores |
| 82 | + CompletableFuture<Void> zrangeWithScores = asyncCommands.zrangeWithScores("racer_scores", 0, -1) |
| 83 | + // REMOVE_START |
| 84 | + .thenApply((List<ScoredValue<String>> res) -> { |
| 85 | + assertThat(res.toString()).isEqualTo("[ScoredValue[6.000000, Ford], ScoredValue[8.000000, Sam-Bodden]," |
| 86 | + + " ScoredValue[10.000000, Norem], ScoredValue[10.000000, Royce]," |
| 87 | + + " ScoredValue[12.000000, Castilla], ScoredValue[14.000000, Prickett]]"); |
| 88 | + return res; |
| 89 | + }) |
| 90 | + // REMOVE_END |
| 91 | + .thenAccept(System.out::println) |
| 92 | + // >>> [ScoredValue[6.000000, Ford], ScoredValue[8.000000, Sam-Bodden]... |
| 93 | + .toCompletableFuture(); |
| 94 | + // STEP_END |
| 95 | + zrangeWithScores.join(); |
| 96 | + |
| 97 | + // STEP_START zrangebyscore |
| 98 | + CompletableFuture<Void> zrangebyscore = asyncCommands |
| 99 | + .zrangebyscore("racer_scores", Range.create(Double.MIN_VALUE, 10)) |
| 100 | + // REMOVE_START |
| 101 | + .thenApply(res -> { |
| 102 | + assertThat(res.toString()).isEqualTo("[Ford, Sam-Bodden, Norem, Royce]"); |
| 103 | + return res; |
| 104 | + }) |
| 105 | + // REMOVE_END |
| 106 | + .thenAccept(System.out::println) |
| 107 | + // >>> [Ford, Sam-Bodden, Norem, Royce] |
| 108 | + .toCompletableFuture(); |
| 109 | + // STEP_END |
| 110 | + zrangebyscore.join(); |
| 111 | + |
| 112 | + // STEP_START zremrangebyscore |
| 113 | + CompletableFuture<Void> zremrangebyscore = asyncCommands.zrem("racer_scores", "Castilla").thenCompose(res4 -> { |
| 114 | + System.out.println(res4); // >>> 1 |
| 115 | + // REMOVE_START |
| 116 | + assertThat(res4).isEqualTo(1); |
| 117 | + // REMOVE_END |
| 118 | + |
| 119 | + return asyncCommands.zremrangebyscore("racer_scores", Range.create(Double.MIN_VALUE, 9)); |
| 120 | + }).thenCompose(res5 -> { |
| 121 | + System.out.println(res5); // >>> 2 |
| 122 | + // REMOVE_START |
| 123 | + assertThat(res5).isEqualTo(2); |
| 124 | + // REMOVE_END |
| 125 | + |
| 126 | + return asyncCommands.zrange("racer_scores", 0, -1); |
| 127 | + }) |
| 128 | + // REMOVE_START |
| 129 | + .thenApply(res -> { |
| 130 | + assertThat(res.toString()).isEqualTo("[Norem, Royce, Prickett]"); |
| 131 | + return res; |
| 132 | + }) |
| 133 | + // REMOVE_END |
| 134 | + .thenAccept(System.out::println) |
| 135 | + // >>> [Norem, Royce, Prickett] |
| 136 | + .toCompletableFuture(); |
| 137 | + // STEP_END |
| 138 | + zremrangebyscore.join(); |
| 139 | + |
| 140 | + // STEP_START zrank |
| 141 | + CompletableFuture<Void> zrank = asyncCommands.zrank("racer_scores", "Norem").thenCompose(res6 -> { |
| 142 | + System.out.println(res6); // >>> 0 |
| 143 | + // REMOVE_START |
| 144 | + assertThat(res6).isZero(); |
| 145 | + // REMOVE_END |
| 146 | + |
| 147 | + return asyncCommands.zrevrank("racer_scores", "Norem"); |
| 148 | + }) |
| 149 | + // REMOVE_START |
| 150 | + .thenApply(res -> { |
| 151 | + assertThat(res).isEqualTo(2); |
| 152 | + return res; |
| 153 | + }) |
| 154 | + // REMOVE_END |
| 155 | + .thenAccept(System.out::println) // >>> 2 |
| 156 | + .toCompletableFuture(); |
| 157 | + // STEP_END |
| 158 | + zrank.join(); |
| 159 | + |
| 160 | + // STEP_START zadd_lex |
| 161 | + CompletableFuture<Void> zaddLex = asyncCommands.zadd("racer_scores", ScoredValue.just(0d, "Norem"), |
| 162 | + ScoredValue.just(0d, "Sam-Bodden"), ScoredValue.just(0d, "Royce"), ScoredValue.just(0d, "Castilla"), |
| 163 | + ScoredValue.just(0d, "Prickett"), ScoredValue.just(0d, "Ford")).thenCompose(res7 -> { |
| 164 | + System.out.println(res7); // >>> 3 |
| 165 | + // REMOVE_START |
| 166 | + assertThat(res7).isEqualTo(3); |
| 167 | + // REMOVE_END |
| 168 | + |
| 169 | + return asyncCommands.zrange("racer_scores", 0, -1); |
| 170 | + }).thenCompose(res8 -> { |
| 171 | + System.out.println(res8); |
| 172 | + // >>> [Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden] |
| 173 | + // REMOVE_START |
| 174 | + assertThat(res8.toString()).isEqualTo("[Castilla, Ford, Norem, Prickett, Royce, Sam-Bodden]"); |
| 175 | + // REMOVE_END |
| 176 | + |
| 177 | + return asyncCommands.zrangebylex("racer_scores", Range.create("A", "L")); |
| 178 | + }) |
| 179 | + // REMOVE_START |
| 180 | + .thenApply(res -> { |
| 181 | + assertThat(res.toString()).isEqualTo("[Castilla, Ford]"); |
| 182 | + return res; |
| 183 | + }) |
| 184 | + // REMOVE_END |
| 185 | + .thenAccept(System.out::println) |
| 186 | + // >>> [Castilla, Ford] |
| 187 | + .toCompletableFuture(); |
| 188 | + // STEP_END |
| 189 | + zaddLex.join(); |
| 190 | + |
| 191 | + // STEP_START leaderboard |
| 192 | + CompletableFuture<Void> leaderboard = asyncCommands.zadd("racer_scores", ScoredValue.just(100, "Wood")) |
| 193 | + .thenCompose(res9 -> { |
| 194 | + System.out.println(res9); // >>> 1 |
| 195 | + // REMOVE_START |
| 196 | + assertThat(res9).isEqualTo(1); |
| 197 | + // REMOVE_END |
| 198 | + |
| 199 | + return asyncCommands.zadd("racer_scores", ScoredValue.just(100, "Henshaw")); |
| 200 | + }).thenCompose(res10 -> { |
| 201 | + System.out.println(res10); // >>> 1 |
| 202 | + // REMOVE_START |
| 203 | + assertThat(res10).isEqualTo(1); |
| 204 | + // REMOVE_END |
| 205 | + |
| 206 | + return asyncCommands.zadd("racer_scores", ScoredValue.just(150, "Henshaw")); |
| 207 | + }).thenCompose(res11 -> { |
| 208 | + System.out.println(res11); // >>> 0 |
| 209 | + // REMOVE_START |
| 210 | + assertThat(res11).isZero(); |
| 211 | + // REMOVE_END |
| 212 | + |
| 213 | + return asyncCommands.zincrby("racer_scores", 50, "Wood"); |
| 214 | + }).thenCompose(res12 -> { |
| 215 | + System.out.println(res12); // >>> 150 |
| 216 | + // REMOVE_START |
| 217 | + assertThat(res12).isEqualTo(150); |
| 218 | + // REMOVE_END |
| 219 | + |
| 220 | + return asyncCommands.zincrby("racer_scores", 50, "Henshaw"); |
| 221 | + }) |
| 222 | + // REMOVE_START |
| 223 | + .thenApply(res -> { |
| 224 | + assertThat(res).isEqualTo(200); |
| 225 | + return res; |
| 226 | + }) |
| 227 | + // REMOVE_END |
| 228 | + .thenAccept(System.out::println) // >>> 200 |
| 229 | + .toCompletableFuture(); |
| 230 | + // STEP_END |
| 231 | + leaderboard.join(); |
| 232 | + // HIDE_START |
| 233 | + } finally { |
| 234 | + redisClient.shutdown(); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | +} |
| 239 | +// HIDE_END |
0 commit comments