|
| 1 | +// EXAMPLE: cmds_list |
| 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 | +import java.util.concurrent.CompletableFuture; |
| 9 | +// REMOVE_START |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | +// REMOVE_END |
| 13 | + |
| 14 | +public class CmdsListExample { |
| 15 | + |
| 16 | + // REMOVE_START |
| 17 | + @Test |
| 18 | + // REMOVE_END |
| 19 | + public void run() { |
| 20 | + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); |
| 21 | + |
| 22 | + try (StatefulRedisConnection<String, String> connection = redisClient.connect()) { |
| 23 | + RedisAsyncCommands<String, String> asyncCommands = connection.async(); |
| 24 | + // REMOVE_START |
| 25 | + asyncCommands.del("mylist").toCompletableFuture().join(); |
| 26 | + // REMOVE_END |
| 27 | + |
| 28 | + // STEP_START lpush |
| 29 | + CompletableFuture<Void> lpush = asyncCommands.lpush("mylist", "world").thenCompose(res1 -> { |
| 30 | + System.out.println(res1); // >>> 1 |
| 31 | + // REMOVE_START |
| 32 | + assertThat(res1).isEqualTo(1); |
| 33 | + // REMOVE_END |
| 34 | + |
| 35 | + return asyncCommands.lpush("mylist", "hello"); |
| 36 | + }).thenCompose(res2 -> { |
| 37 | + System.out.println(res2); // >>> 2 |
| 38 | + // REMOVE_START |
| 39 | + assertThat(res2).isEqualTo(2); |
| 40 | + // REMOVE_END |
| 41 | + |
| 42 | + return asyncCommands.lrange("mylist", 0, -1); |
| 43 | + }) |
| 44 | + // REMOVE_START |
| 45 | + .thenApply(res3 -> { |
| 46 | + assertThat(res3.toString()).isEqualTo("[hello, world]"); |
| 47 | + return res3; |
| 48 | + }) |
| 49 | + // REMOVE_END |
| 50 | + .thenAccept(res3 -> System.out.println(res3)) // >>> [hello, world] |
| 51 | + .toCompletableFuture(); |
| 52 | + // STEP_END |
| 53 | + lpush.join(); |
| 54 | + |
| 55 | + // REMOVE_START |
| 56 | + asyncCommands.del("mylist").toCompletableFuture().join(); |
| 57 | + // REMOVE_END |
| 58 | + |
| 59 | + // STEP_START lrange |
| 60 | + CompletableFuture<Void> lrange = asyncCommands.rpush("mylist", "one").thenCompose(res4 -> { |
| 61 | + System.out.println(res4); // >>> 1 |
| 62 | + // REMOVE_START |
| 63 | + assertThat(res4).isEqualTo(1); |
| 64 | + // REMOVE_END |
| 65 | + |
| 66 | + return asyncCommands.rpush("mylist", "two"); |
| 67 | + }).thenCompose(res5 -> { |
| 68 | + System.out.println(res5); // >>> 2 |
| 69 | + // REMOVE_START |
| 70 | + assertThat(res5).isEqualTo(2); |
| 71 | + // REMOVE_END |
| 72 | + |
| 73 | + return asyncCommands.rpush("mylist", "three"); |
| 74 | + }).thenCompose(res6 -> { |
| 75 | + System.out.println(res6); // >>> 3 |
| 76 | + // REMOVE_START |
| 77 | + assertThat(res6).isEqualTo(3); |
| 78 | + // REMOVE_END |
| 79 | + |
| 80 | + return asyncCommands.lrange("mylist", 0, 0); |
| 81 | + }).thenCompose(res7 -> { |
| 82 | + System.out.println(res7); // >>> [one] |
| 83 | + // REMOVE_START |
| 84 | + assertThat(res7.toString()).isEqualTo("[one]"); |
| 85 | + // REMOVE_END |
| 86 | + |
| 87 | + return asyncCommands.lrange("mylist", -3, 2); |
| 88 | + }).thenCompose(res8 -> { |
| 89 | + System.out.println(res8); // >>> [one, two, three] |
| 90 | + // REMOVE_START |
| 91 | + assertThat(res8.toString()).isEqualTo("[one, two, three]"); |
| 92 | + // REMOVE_END |
| 93 | + |
| 94 | + return asyncCommands.lrange("mylist", -100, 100); |
| 95 | + }).thenCompose(res9 -> { |
| 96 | + System.out.println(res9); // >>> [one, two, three] |
| 97 | + // REMOVE_START |
| 98 | + assertThat(res9.toString()).isEqualTo("[one, two, three]"); |
| 99 | + // REMOVE_END |
| 100 | + |
| 101 | + return asyncCommands.lrange("mylist", 5, 10); |
| 102 | + }) |
| 103 | + // REMOVE_START |
| 104 | + .thenApply(res10 -> { |
| 105 | + assertThat(res10.toString()).isEqualTo("[]"); |
| 106 | + return res10; |
| 107 | + }) |
| 108 | + // REMOVE_END |
| 109 | + .thenAccept(res10 -> System.out.println(res10)) // >>> [] |
| 110 | + .toCompletableFuture(); |
| 111 | + // STEP_END |
| 112 | + lrange.join(); |
| 113 | + |
| 114 | + // REMOVE_START |
| 115 | + asyncCommands.del("mylist").toCompletableFuture().join(); |
| 116 | + // REMOVE_END |
| 117 | + |
| 118 | + // STEP_START llen |
| 119 | + CompletableFuture<Void> llen = asyncCommands.lpush("mylist", "World").thenCompose(res11 -> { |
| 120 | + System.out.println(res11); // >>> 1 |
| 121 | + // REMOVE_START |
| 122 | + assertThat(res11).isEqualTo(1); |
| 123 | + // REMOVE_END |
| 124 | + |
| 125 | + return asyncCommands.lpush("mylist", "Hello"); |
| 126 | + }).thenCompose(res12 -> { |
| 127 | + System.out.println(res12); // >>> 2 |
| 128 | + // REMOVE_START |
| 129 | + assertThat(res12).isEqualTo(2); |
| 130 | + // REMOVE_END |
| 131 | + |
| 132 | + return asyncCommands.llen("mylist"); |
| 133 | + }) |
| 134 | + // REMOVE_START |
| 135 | + .thenApply(res13 -> { |
| 136 | + assertThat(res13).isEqualTo(2); |
| 137 | + return res13; |
| 138 | + }) |
| 139 | + // REMOVE_END |
| 140 | + .thenAccept(res13 -> System.out.println(res13)) // >>> 2 |
| 141 | + .toCompletableFuture(); |
| 142 | + // STEP_END |
| 143 | + llen.join(); |
| 144 | + |
| 145 | + // REMOVE_START |
| 146 | + asyncCommands.del("mylist").toCompletableFuture().join(); |
| 147 | + // REMOVE_END |
| 148 | + |
| 149 | + // STEP_START rpush |
| 150 | + CompletableFuture<Void> rpush = asyncCommands.rpush("mylist", "hello").thenCompose(res14 -> { |
| 151 | + System.out.println(res14); // >>> 1 |
| 152 | + // REMOVE_START |
| 153 | + assertThat(res14).isEqualTo(1); |
| 154 | + // REMOVE_END |
| 155 | + |
| 156 | + return asyncCommands.rpush("mylist", "world"); |
| 157 | + }).thenCompose(res15 -> { |
| 158 | + System.out.println(res15); // >>> 2 |
| 159 | + // REMOVE_START |
| 160 | + assertThat(res15).isEqualTo(2); |
| 161 | + // REMOVE_END |
| 162 | + |
| 163 | + return asyncCommands.lrange("mylist", 0, -1); |
| 164 | + }) |
| 165 | + // REMOVE_START |
| 166 | + .thenApply(res16 -> { |
| 167 | + assertThat(res16.toString()).isEqualTo("[hello, world]"); |
| 168 | + return res16; |
| 169 | + }) |
| 170 | + // REMOVE_END |
| 171 | + .thenAccept(res16 -> System.out.println(res16)) // >>> [hello, world] |
| 172 | + .toCompletableFuture(); |
| 173 | + // STEP_END |
| 174 | + rpush.join(); |
| 175 | + |
| 176 | + // REMOVE_START |
| 177 | + asyncCommands.del("mylist").toCompletableFuture().join(); |
| 178 | + // REMOVE_END |
| 179 | + |
| 180 | + // STEP_START lpop |
| 181 | + CompletableFuture<Void> lpop = asyncCommands.rpush("mylist", "one", "two", "three", "four", "five") |
| 182 | + .thenCompose(res17 -> { |
| 183 | + System.out.println(res17); // >>> 5 |
| 184 | + // REMOVE_START |
| 185 | + assertThat(res17).isEqualTo(5); |
| 186 | + // REMOVE_END |
| 187 | + |
| 188 | + return asyncCommands.lpop("mylist"); |
| 189 | + }).thenCompose(res18 -> { |
| 190 | + System.out.println(res18); // >>> one |
| 191 | + // REMOVE_START |
| 192 | + assertThat(res18).isEqualTo("one"); |
| 193 | + // REMOVE_END |
| 194 | + |
| 195 | + return asyncCommands.lpop("mylist", 2); |
| 196 | + }).thenCompose(res19 -> { |
| 197 | + System.out.println(res19); // >>> [two, three] |
| 198 | + // REMOVE_START |
| 199 | + assertThat(res19.toString()).isEqualTo("[two, three]"); |
| 200 | + // REMOVE_END |
| 201 | + |
| 202 | + return asyncCommands.lrange("mylist", 0, -1); |
| 203 | + }) |
| 204 | + // REMOVE_START |
| 205 | + .thenApply(res17_final -> { |
| 206 | + assertThat(res17_final.toString()).isEqualTo("[four, five]"); |
| 207 | + return res17_final; |
| 208 | + }) |
| 209 | + // REMOVE_END |
| 210 | + .thenAccept(res17_final -> System.out.println(res17_final)) // >>> [four, five] |
| 211 | + .toCompletableFuture(); |
| 212 | + // STEP_END |
| 213 | + lpop.join(); |
| 214 | + |
| 215 | + // REMOVE_START |
| 216 | + asyncCommands.del("mylist").toCompletableFuture().join(); |
| 217 | + // REMOVE_END |
| 218 | + |
| 219 | + // STEP_START rpop |
| 220 | + CompletableFuture<Void> rpop = asyncCommands.rpush("mylist", "one", "two", "three", "four", "five") |
| 221 | + .thenCompose(res18 -> { |
| 222 | + System.out.println(res18); // >>> 5 |
| 223 | + // REMOVE_START |
| 224 | + assertThat(res18).isEqualTo(5); |
| 225 | + // REMOVE_END |
| 226 | + |
| 227 | + return asyncCommands.rpop("mylist"); |
| 228 | + }).thenCompose(res19 -> { |
| 229 | + System.out.println(res19); // >>> five |
| 230 | + // REMOVE_START |
| 231 | + assertThat(res19).isEqualTo("five"); |
| 232 | + // REMOVE_END |
| 233 | + |
| 234 | + return asyncCommands.rpop("mylist", 2); |
| 235 | + }).thenCompose(res20 -> { |
| 236 | + System.out.println(res20); // >>> [four, three] |
| 237 | + // REMOVE_START |
| 238 | + assertThat(res20.toString()).isEqualTo("[four, three]"); |
| 239 | + // REMOVE_END |
| 240 | + |
| 241 | + return asyncCommands.lrange("mylist", 0, -1); |
| 242 | + }) |
| 243 | + // REMOVE_START |
| 244 | + .thenApply(res21 -> { |
| 245 | + assertThat(res21.toString()).isEqualTo("[one, two]"); |
| 246 | + return res21; |
| 247 | + }) |
| 248 | + // REMOVE_END |
| 249 | + .thenAccept(res21 -> System.out.println(res21)) // >>> [one, two] |
| 250 | + .toCompletableFuture(); |
| 251 | + // STEP_END |
| 252 | + rpop.join(); |
| 253 | + |
| 254 | + // REMOVE_START |
| 255 | + asyncCommands.del("mylist").toCompletableFuture().join(); |
| 256 | + // REMOVE_END |
| 257 | + |
| 258 | + // HIDE_START |
| 259 | + } finally { |
| 260 | + redisClient.shutdown(); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | +} |
| 265 | +// HIDE_END |
0 commit comments