|
| 1 | +// EXAMPLE: cmds_list |
| 2 | +package io.redis.examples.reactive; |
| 3 | + |
| 4 | +import io.lettuce.core.*; |
| 5 | +import io.lettuce.core.api.reactive.RedisReactiveCommands; |
| 6 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 7 | + |
| 8 | +// REMOVE_START |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +// REMOVE_END |
| 12 | + |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | + |
| 15 | +public class CmdsListExample { |
| 16 | + |
| 17 | + // REMOVE_START |
| 18 | + @Test |
| 19 | + // REMOVE_END |
| 20 | + public void run() { |
| 21 | + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); |
| 22 | + |
| 23 | + try (StatefulRedisConnection<String, String> connection = redisClient.connect()) { |
| 24 | + RedisReactiveCommands<String, String> reactiveCommands = connection.reactive(); |
| 25 | + // REMOVE_START |
| 26 | + reactiveCommands.del("mylist").block(); |
| 27 | + // REMOVE_END |
| 28 | + |
| 29 | + // STEP_START lpush |
| 30 | + Mono<Void> lpush = reactiveCommands.lpush("mylist", "world").doOnNext(res1 -> { |
| 31 | + System.out.println(res1); // >>> 1 |
| 32 | + // REMOVE_START |
| 33 | + assertThat(res1).isEqualTo(1); |
| 34 | + // REMOVE_END |
| 35 | + }).flatMap(res1 -> reactiveCommands.lpush("mylist", "hello")).doOnNext(res2 -> { |
| 36 | + System.out.println(res2); // >>> 2 |
| 37 | + // REMOVE_START |
| 38 | + assertThat(res2).isEqualTo(2); |
| 39 | + // REMOVE_END |
| 40 | + }).flatMap(res2 -> reactiveCommands.lrange("mylist", 0, -1).collectList()).doOnNext(res3 -> { |
| 41 | + System.out.println(res3); // >>> [hello, world] |
| 42 | + // REMOVE_START |
| 43 | + assertThat(res3.toArray()).isEqualTo(new String[] { "hello", "world" }); |
| 44 | + // REMOVE_END |
| 45 | + }).then(); |
| 46 | + // STEP_END |
| 47 | + lpush.block(); |
| 48 | + reactiveCommands.del("mylist").block(); |
| 49 | + |
| 50 | + // STEP_START lrange |
| 51 | + Mono<Void> lrange = reactiveCommands.rpush("mylist", "one").doOnNext(res4 -> { |
| 52 | + System.out.println(res4); // >>> 1 |
| 53 | + // REMOVE_START |
| 54 | + assertThat(res4).isEqualTo(1); |
| 55 | + // REMOVE_END |
| 56 | + }).flatMap(res4 -> reactiveCommands.rpush("mylist", "two")).doOnNext(res5 -> { |
| 57 | + System.out.println(res5); // >>> 2 |
| 58 | + // REMOVE_START |
| 59 | + assertThat(res5).isEqualTo(2); |
| 60 | + // REMOVE_END |
| 61 | + }).flatMap(res5 -> reactiveCommands.rpush("mylist", "three")).doOnNext(res6 -> { |
| 62 | + System.out.println(res6); // >>> 3 |
| 63 | + // REMOVE_START |
| 64 | + assertThat(res6).isEqualTo(3); |
| 65 | + // REMOVE_END |
| 66 | + }).flatMap(res6 -> reactiveCommands.lrange("mylist", 0, 0).collectList()).doOnNext(res7 -> { |
| 67 | + System.out.println(res7); // >>> [one] |
| 68 | + // REMOVE_START |
| 69 | + assertThat(res7.toArray()).isEqualTo(new String[] { "one" }); |
| 70 | + // REMOVE_END |
| 71 | + }).flatMap(res7 -> reactiveCommands.lrange("mylist", -3, 2).collectList()).doOnNext(res8 -> { |
| 72 | + System.out.println(res8); // >>> [one, two, three] |
| 73 | + // REMOVE_START |
| 74 | + assertThat(res8.toArray()).isEqualTo(new String[] { "one", "two", "three" }); |
| 75 | + // REMOVE_END |
| 76 | + }).flatMap(res8 -> reactiveCommands.lrange("mylist", -100, 100).collectList()).doOnNext(res9 -> { |
| 77 | + System.out.println(res9); // >>> [one, two, three] |
| 78 | + // REMOVE_START |
| 79 | + assertThat(res9.toArray()).isEqualTo(new String[] { "one", "two", "three" }); |
| 80 | + // REMOVE_END |
| 81 | + }).flatMap(res9 -> reactiveCommands.lrange("mylist", 5, 10).collectList()).doOnNext(res10 -> { |
| 82 | + System.out.println(res10); // >>> [] |
| 83 | + // REMOVE_START |
| 84 | + assertThat(res10.toArray()).isEqualTo(new String[] {}); |
| 85 | + // REMOVE_END |
| 86 | + }).then(); |
| 87 | + // STEP_END |
| 88 | + lrange.block(); |
| 89 | + reactiveCommands.del("mylist").block(); |
| 90 | + |
| 91 | + // STEP_START llen |
| 92 | + Mono<Void> llen = reactiveCommands.lpush("mylist", "World").doOnNext(res11 -> { |
| 93 | + System.out.println(res11); // >>> 1 |
| 94 | + // REMOVE_START |
| 95 | + assertThat(res11).isEqualTo(1); |
| 96 | + // REMOVE_END |
| 97 | + }).flatMap(res11 -> reactiveCommands.lpush("mylist", "Hello")).doOnNext(res12 -> { |
| 98 | + System.out.println(res12); // >>> 2 |
| 99 | + // REMOVE_START |
| 100 | + assertThat(res12).isEqualTo(2); |
| 101 | + // REMOVE_END |
| 102 | + }).flatMap(res12 -> reactiveCommands.llen("mylist")).doOnNext(res13 -> { |
| 103 | + System.out.println(res13); // >>> 2 |
| 104 | + // REMOVE_START |
| 105 | + assertThat(res13).isEqualTo(2); |
| 106 | + // REMOVE_END |
| 107 | + }).then(); |
| 108 | + // STEP_END |
| 109 | + llen.block(); |
| 110 | + reactiveCommands.del("mylist").block(); |
| 111 | + |
| 112 | + // STEP_START rpush |
| 113 | + Mono<Void> rpush = reactiveCommands.rpush("mylist", "hello").doOnNext(res14 -> { |
| 114 | + System.out.println(res14); // >>> 1 |
| 115 | + // REMOVE_START |
| 116 | + assertThat(res14).isEqualTo(1); |
| 117 | + // REMOVE_END |
| 118 | + }).flatMap(res14 -> reactiveCommands.rpush("mylist", "world")).doOnNext(res15 -> { |
| 119 | + System.out.println(res15); // >>> 2 |
| 120 | + // REMOVE_START |
| 121 | + assertThat(res15).isEqualTo(2); |
| 122 | + // REMOVE_END |
| 123 | + }).flatMap(res15 -> reactiveCommands.lrange("mylist", 0, -1).collectList()).doOnNext(res16 -> { |
| 124 | + System.out.println(res16); // >>> [hello, world] |
| 125 | + // REMOVE_START |
| 126 | + assertThat(res16.toArray()).isEqualTo(new String[] { "hello", "world" }); |
| 127 | + // REMOVE_END |
| 128 | + }).then(); |
| 129 | + // STEP_END |
| 130 | + rpush.block(); |
| 131 | + reactiveCommands.del("mylist").block(); |
| 132 | + |
| 133 | + // STEP_START lpop |
| 134 | + Mono<Void> lpop = reactiveCommands.rpush("mylist", "one", "two", "three", "four", "five").doOnNext(res17 -> { |
| 135 | + System.out.println(res17); // >>> 5 |
| 136 | + // REMOVE_START |
| 137 | + assertThat(res17).isEqualTo(5); |
| 138 | + // REMOVE_END |
| 139 | + }).flatMap(res17 -> reactiveCommands.lpop("mylist")).doOnNext(res18 -> { |
| 140 | + System.out.println(res18); // >>> one |
| 141 | + // REMOVE_START |
| 142 | + assertThat(res18).isEqualTo("one"); |
| 143 | + // REMOVE_END |
| 144 | + }).flatMap(res18 -> reactiveCommands.lpop("mylist", 2).collectList()).doOnNext(res19 -> { |
| 145 | + System.out.println(res19); // >>> [two, three] |
| 146 | + // REMOVE_START |
| 147 | + assertThat(res19.toArray()).isEqualTo(new String[] { "two", "three" }); |
| 148 | + // REMOVE_END |
| 149 | + }).flatMap(res19 -> reactiveCommands.lrange("mylist", 0, -1).collectList()).doOnNext(res17_final -> { |
| 150 | + System.out.println(res17_final); // >>> [four, five] |
| 151 | + // REMOVE_START |
| 152 | + assertThat(res17_final.toArray()).isEqualTo(new String[] { "four", "five" }); |
| 153 | + // REMOVE_END |
| 154 | + }).then(); |
| 155 | + // STEP_END |
| 156 | + lpop.block(); |
| 157 | + reactiveCommands.del("mylist").block(); |
| 158 | + |
| 159 | + // STEP_START rpop |
| 160 | + Mono<Void> rpop = reactiveCommands.rpush("mylist", "one", "two", "three", "four", "five").doOnNext(res18 -> { |
| 161 | + System.out.println(res18); // >>> 5 |
| 162 | + // REMOVE_START |
| 163 | + assertThat(res18).isEqualTo(5); |
| 164 | + // REMOVE_END |
| 165 | + }).flatMap(res18 -> reactiveCommands.rpop("mylist")).doOnNext(res19 -> { |
| 166 | + System.out.println(res19); // >>> five |
| 167 | + // REMOVE_START |
| 168 | + assertThat(res19).isEqualTo("five"); |
| 169 | + // REMOVE_END |
| 170 | + }).flatMap(res19 -> reactiveCommands.rpop("mylist", 2).collectList()).doOnNext(res20 -> { |
| 171 | + System.out.println(res20); // >>> [four, three] |
| 172 | + // REMOVE_START |
| 173 | + assertThat(res20.toArray()).isEqualTo(new String[] { "four", "three" }); |
| 174 | + // REMOVE_END |
| 175 | + }).flatMap(res20 -> reactiveCommands.lrange("mylist", 0, -1).collectList()).doOnNext(res21 -> { |
| 176 | + System.out.println(res21); // >>> [one, two] |
| 177 | + // REMOVE_START |
| 178 | + assertThat(res21.toArray()).isEqualTo(new String[] { "one", "two" }); |
| 179 | + // REMOVE_END |
| 180 | + }).then(); |
| 181 | + // STEP_END |
| 182 | + rpop.block(); |
| 183 | + reactiveCommands.del("mylist").block(); |
| 184 | + |
| 185 | + } finally { |
| 186 | + redisClient.shutdown(); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments