|
| 1 | +// EXAMPLE: cmds_sorted_set |
| 2 | +// HIDE_START |
| 3 | +package example_commands_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/redis/go-redis/v9" |
| 10 | +) |
| 11 | + |
| 12 | +// HIDE_END |
| 13 | + |
| 14 | +func ExampleClient_zadd_cmd() { |
| 15 | + ctx := context.Background() |
| 16 | + |
| 17 | + rdb := redis.NewClient(&redis.Options{ |
| 18 | + Addr: "localhost:6379", |
| 19 | + Password: "", // no password docs |
| 20 | + DB: 0, // use default DB |
| 21 | + }) |
| 22 | + |
| 23 | + // REMOVE_START |
| 24 | + rdb.Del(ctx, "myzset") |
| 25 | + // REMOVE_END |
| 26 | + |
| 27 | + // STEP_START zadd |
| 28 | + zAddResult1, err := rdb.ZAdd(ctx, "myzset", |
| 29 | + redis.Z{Member: "one", Score: 1}, |
| 30 | + ).Result() |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + panic(err) |
| 34 | + } |
| 35 | + |
| 36 | + fmt.Println(zAddResult1) // >>> 1 |
| 37 | + |
| 38 | + zAddResult2, err := rdb.ZAdd(ctx, "myzset", |
| 39 | + redis.Z{Member: "uno", Score: 1}, |
| 40 | + ).Result() |
| 41 | + |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + |
| 46 | + fmt.Println(zAddResult2) |
| 47 | + |
| 48 | + zAddResult3, err := rdb.ZAdd(ctx, "myzset", |
| 49 | + redis.Z{Member: "two", Score: 2}, |
| 50 | + redis.Z{Member: "three", Score: 3}, |
| 51 | + ).Result() |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Println(zAddResult3) // >>> 2 |
| 58 | + |
| 59 | + zAddResult4, err := rdb.ZRangeWithScores(ctx, "myzset", 0, -1).Result() |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + |
| 65 | + fmt.Println(zAddResult4) // >>> [{1 one} {1 uno} {2 two} {3 three}] |
| 66 | + // STEP_END |
| 67 | + |
| 68 | + // Output: |
| 69 | + // 1 |
| 70 | + // 1 |
| 71 | + // 2 |
| 72 | + // [{1 one} {1 uno} {2 two} {3 three}] |
| 73 | +} |
| 74 | + |
| 75 | +func ExampleClient_zrange1() { |
| 76 | + ctx := context.Background() |
| 77 | + |
| 78 | + rdb := redis.NewClient(&redis.Options{ |
| 79 | + Addr: "localhost:6379", |
| 80 | + Password: "", // no password docs |
| 81 | + DB: 0, // use default DB |
| 82 | + }) |
| 83 | + |
| 84 | + // REMOVE_START |
| 85 | + rdb.Del(ctx, "myzset") |
| 86 | + // REMOVE_END |
| 87 | + |
| 88 | + // STEP_START zrange1 |
| 89 | + zrangeResult1, err := rdb.ZAdd(ctx, "myzset", |
| 90 | + redis.Z{Member: "one", Score: 1}, |
| 91 | + redis.Z{Member: "two", Score: 2}, |
| 92 | + redis.Z{Member: "three", Score: 3}, |
| 93 | + ).Result() |
| 94 | + |
| 95 | + if err != nil { |
| 96 | + panic(err) |
| 97 | + } |
| 98 | + |
| 99 | + fmt.Println(zrangeResult1) // >>> 3 |
| 100 | + |
| 101 | + zrangeResult2, err := rdb.ZRange(ctx, "myzset", 0, -1).Result() |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + panic(err) |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Println(zrangeResult2) // >>> [one two three] |
| 108 | + |
| 109 | + zrangeResult3, err := rdb.ZRange(ctx, "myzset", 2, 3).Result() |
| 110 | + |
| 111 | + if err != nil { |
| 112 | + panic(err) |
| 113 | + } |
| 114 | + |
| 115 | + fmt.Println(zrangeResult3) // >>> [three] |
| 116 | + |
| 117 | + zrangeResult4, err := rdb.ZRange(ctx, "myzset", -2, -1).Result() |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + panic(err) |
| 121 | + } |
| 122 | + |
| 123 | + fmt.Println(zrangeResult4) // >>> [two three] |
| 124 | + // STEP_END |
| 125 | + |
| 126 | + // Output: |
| 127 | + // 3 |
| 128 | + // [one two three] |
| 129 | + // [three] |
| 130 | + // [two three] |
| 131 | +} |
| 132 | + |
| 133 | +func ExampleClient_zrange2() { |
| 134 | + ctx := context.Background() |
| 135 | + |
| 136 | + rdb := redis.NewClient(&redis.Options{ |
| 137 | + Addr: "localhost:6379", |
| 138 | + Password: "", // no password docs |
| 139 | + DB: 0, // use default DB |
| 140 | + }) |
| 141 | + |
| 142 | + // REMOVE_START |
| 143 | + rdb.Del(ctx, "myzset") |
| 144 | + // REMOVE_END |
| 145 | + |
| 146 | + // STEP_START zrange2 |
| 147 | + zRangeResult5, err := rdb.ZAdd(ctx, "myzset", |
| 148 | + redis.Z{Member: "one", Score: 1}, |
| 149 | + redis.Z{Member: "two", Score: 2}, |
| 150 | + redis.Z{Member: "three", Score: 3}, |
| 151 | + ).Result() |
| 152 | + |
| 153 | + if err != nil { |
| 154 | + panic(err) |
| 155 | + } |
| 156 | + |
| 157 | + fmt.Println(zRangeResult5) // >>> 3 |
| 158 | + |
| 159 | + zRangeResult6, err := rdb.ZRangeWithScores(ctx, "myzset", 0, 1).Result() |
| 160 | + |
| 161 | + if err != nil { |
| 162 | + panic(err) |
| 163 | + } |
| 164 | + |
| 165 | + fmt.Println(zRangeResult6) // >>> [{1 one} {2 two}] |
| 166 | + // STEP_END |
| 167 | + |
| 168 | + // Output: |
| 169 | + // 3 |
| 170 | + // [{1 one} {2 two}] |
| 171 | +} |
| 172 | + |
| 173 | +func ExampleClient_zrange3() { |
| 174 | + ctx := context.Background() |
| 175 | + |
| 176 | + rdb := redis.NewClient(&redis.Options{ |
| 177 | + Addr: "localhost:6379", |
| 178 | + Password: "", // no password docs |
| 179 | + DB: 0, // use default DB |
| 180 | + }) |
| 181 | + |
| 182 | + // REMOVE_START |
| 183 | + rdb.Del(ctx, "myzset") |
| 184 | + // REMOVE_END |
| 185 | + |
| 186 | + // STEP_START zrange3 |
| 187 | + zRangeResult7, err := rdb.ZAdd(ctx, "myzset", |
| 188 | + redis.Z{Member: "one", Score: 1}, |
| 189 | + redis.Z{Member: "two", Score: 2}, |
| 190 | + redis.Z{Member: "three", Score: 3}, |
| 191 | + ).Result() |
| 192 | + |
| 193 | + if err != nil { |
| 194 | + panic(err) |
| 195 | + } |
| 196 | + |
| 197 | + fmt.Println(zRangeResult7) // >>> 3 |
| 198 | + |
| 199 | + zRangeResult8, err := rdb.ZRangeArgs(ctx, |
| 200 | + redis.ZRangeArgs{ |
| 201 | + Key: "myzset", |
| 202 | + ByScore: true, |
| 203 | + Start: "(1", |
| 204 | + Stop: "+inf", |
| 205 | + Offset: 1, |
| 206 | + Count: 1, |
| 207 | + }, |
| 208 | + ).Result() |
| 209 | + |
| 210 | + if err != nil { |
| 211 | + panic(err) |
| 212 | + } |
| 213 | + |
| 214 | + fmt.Println(zRangeResult8) // >>> [three] |
| 215 | + // STEP_END |
| 216 | + |
| 217 | + // Output: |
| 218 | + // 3 |
| 219 | + // [three] |
| 220 | +} |
0 commit comments