|
| 1 | +// EXAMPLE: hash_tutorial |
| 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_set_get_all() { |
| 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, "bike:1") |
| 25 | + // REMOVE_END |
| 26 | + |
| 27 | + // STEP_START set_get_all |
| 28 | + hashFields := []string{ |
| 29 | + "model", "Deimos", |
| 30 | + "brand", "Ergonom", |
| 31 | + "type", "Enduro bikes", |
| 32 | + "price", "4972", |
| 33 | + } |
| 34 | + |
| 35 | + res1, err := rdb.HSet(ctx, "bike:1", hashFields).Result() |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + |
| 41 | + fmt.Println(res1) // >>> 4 |
| 42 | + |
| 43 | + res2, err := rdb.HGet(ctx, "bike:1", "model").Result() |
| 44 | + |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + |
| 49 | + fmt.Println(res2) // >>> Deimos |
| 50 | + |
| 51 | + res3, err := rdb.HGet(ctx, "bike:1", "price").Result() |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Println(res3) // >>> 4972 |
| 58 | + |
| 59 | + cmdReturn := rdb.HGetAll(ctx, "bike:1") |
| 60 | + res4, err := cmdReturn.Result() |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Println(res4) |
| 67 | + // >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes] |
| 68 | + |
| 69 | + type BikeInfo struct { |
| 70 | + Model string `redis:"model"` |
| 71 | + Brand string `redis:"brand"` |
| 72 | + Type string `redis:"type"` |
| 73 | + Price int `redis:"price"` |
| 74 | + } |
| 75 | + |
| 76 | + var res4a BikeInfo |
| 77 | + |
| 78 | + if err := cmdReturn.Scan(&res4a); err != nil { |
| 79 | + panic(err) |
| 80 | + } |
| 81 | + |
| 82 | + fmt.Printf("Model: %v, Brand: %v, Type: %v, Price: $%v\n", |
| 83 | + res4a.Model, res4a.Brand, res4a.Type, res4a.Price) |
| 84 | + // >>> Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972 |
| 85 | + // STEP_END |
| 86 | + |
| 87 | + // Output: |
| 88 | + // 4 |
| 89 | + // Deimos |
| 90 | + // 4972 |
| 91 | + // map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes] |
| 92 | + // Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972 |
| 93 | +} |
| 94 | + |
| 95 | +func ExampleClient_hmget() { |
| 96 | + ctx := context.Background() |
| 97 | + |
| 98 | + rdb := redis.NewClient(&redis.Options{ |
| 99 | + Addr: "localhost:6379", |
| 100 | + Password: "", // no password docs |
| 101 | + DB: 0, // use default DB |
| 102 | + }) |
| 103 | + |
| 104 | + // REMOVE_START |
| 105 | + rdb.Del(ctx, "bike:1") |
| 106 | + // REMOVE_END |
| 107 | + |
| 108 | + hashFields := []string{ |
| 109 | + "model", "Deimos", |
| 110 | + "brand", "Ergonom", |
| 111 | + "type", "Enduro bikes", |
| 112 | + "price", "4972", |
| 113 | + } |
| 114 | + |
| 115 | + _, err := rdb.HSet(ctx, "bike:1", hashFields).Result() |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + panic(err) |
| 119 | + } |
| 120 | + |
| 121 | + // STEP_START hmget |
| 122 | + cmdReturn := rdb.HMGet(ctx, "bike:1", "model", "price") |
| 123 | + res5, err := cmdReturn.Result() |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + panic(err) |
| 127 | + } |
| 128 | + |
| 129 | + fmt.Println(res5) // >>> [Deimos 4972] |
| 130 | + |
| 131 | + type BikeInfo struct { |
| 132 | + Model string `redis:"model"` |
| 133 | + Brand string `redis:"-"` |
| 134 | + Type string `redis:"-"` |
| 135 | + Price int `redis:"price"` |
| 136 | + } |
| 137 | + |
| 138 | + var res5a BikeInfo |
| 139 | + |
| 140 | + if err := cmdReturn.Scan(&res5a); err != nil { |
| 141 | + panic(err) |
| 142 | + } |
| 143 | + |
| 144 | + fmt.Printf("Model: %v, Price: $%v\n", res5a.Model, res5a.Price) |
| 145 | + // >>> Model: Deimos, Price: $4972 |
| 146 | + // STEP_END |
| 147 | + |
| 148 | + // Output: |
| 149 | + // [Deimos 4972] |
| 150 | + // Model: Deimos, Price: $4972 |
| 151 | +} |
| 152 | + |
| 153 | +func ExampleClient_hincrby() { |
| 154 | + ctx := context.Background() |
| 155 | + |
| 156 | + rdb := redis.NewClient(&redis.Options{ |
| 157 | + Addr: "localhost:6379", |
| 158 | + Password: "", // no password docs |
| 159 | + DB: 0, // use default DB |
| 160 | + }) |
| 161 | + |
| 162 | + // REMOVE_START |
| 163 | + rdb.Del(ctx, "bike:1") |
| 164 | + // REMOVE_END |
| 165 | + |
| 166 | + hashFields := []string{ |
| 167 | + "model", "Deimos", |
| 168 | + "brand", "Ergonom", |
| 169 | + "type", "Enduro bikes", |
| 170 | + "price", "4972", |
| 171 | + } |
| 172 | + |
| 173 | + _, err := rdb.HSet(ctx, "bike:1", hashFields).Result() |
| 174 | + |
| 175 | + if err != nil { |
| 176 | + panic(err) |
| 177 | + } |
| 178 | + |
| 179 | + // STEP_START hincrby |
| 180 | + res6, err := rdb.HIncrBy(ctx, "bike:1", "price", 100).Result() |
| 181 | + |
| 182 | + if err != nil { |
| 183 | + panic(err) |
| 184 | + } |
| 185 | + |
| 186 | + fmt.Println(res6) // >>> 5072 |
| 187 | + |
| 188 | + res7, err := rdb.HIncrBy(ctx, "bike:1", "price", -100).Result() |
| 189 | + |
| 190 | + if err != nil { |
| 191 | + panic(err) |
| 192 | + } |
| 193 | + |
| 194 | + fmt.Println(res7) // >>> 4972 |
| 195 | + // STEP_END |
| 196 | + |
| 197 | + // Output: |
| 198 | + // 5072 |
| 199 | + // 4972 |
| 200 | +} |
| 201 | + |
| 202 | +func ExampleClient_incrby_get_mget() { |
| 203 | + ctx := context.Background() |
| 204 | + |
| 205 | + rdb := redis.NewClient(&redis.Options{ |
| 206 | + Addr: "localhost:6379", |
| 207 | + Password: "", // no password docs |
| 208 | + DB: 0, // use default DB |
| 209 | + }) |
| 210 | + |
| 211 | + // REMOVE_START |
| 212 | + rdb.Del(ctx, "bike:1:stats") |
| 213 | + // REMOVE_END |
| 214 | + |
| 215 | + // STEP_START incrby_get_mget |
| 216 | + res8, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result() |
| 217 | + |
| 218 | + if err != nil { |
| 219 | + panic(err) |
| 220 | + } |
| 221 | + |
| 222 | + fmt.Println(res8) // >>> 1 |
| 223 | + |
| 224 | + res9, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result() |
| 225 | + |
| 226 | + if err != nil { |
| 227 | + panic(err) |
| 228 | + } |
| 229 | + |
| 230 | + fmt.Println(res9) // >>> 2 |
| 231 | + |
| 232 | + res10, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result() |
| 233 | + |
| 234 | + if err != nil { |
| 235 | + panic(err) |
| 236 | + } |
| 237 | + |
| 238 | + fmt.Println(res10) // >>> 3 |
| 239 | + |
| 240 | + res11, err := rdb.HIncrBy(ctx, "bike:1:stats", "crashes", 1).Result() |
| 241 | + |
| 242 | + if err != nil { |
| 243 | + panic(err) |
| 244 | + } |
| 245 | + |
| 246 | + fmt.Println(res11) // >>> 1 |
| 247 | + |
| 248 | + res12, err := rdb.HIncrBy(ctx, "bike:1:stats", "owners", 1).Result() |
| 249 | + |
| 250 | + if err != nil { |
| 251 | + panic(err) |
| 252 | + } |
| 253 | + |
| 254 | + fmt.Println(res12) // >>> 1 |
| 255 | + |
| 256 | + res13, err := rdb.HGet(ctx, "bike:1:stats", "rides").Result() |
| 257 | + |
| 258 | + if err != nil { |
| 259 | + panic(err) |
| 260 | + } |
| 261 | + |
| 262 | + fmt.Println(res13) // >>> 3 |
| 263 | + |
| 264 | + res14, err := rdb.HMGet(ctx, "bike:1:stats", "crashes", "owners").Result() |
| 265 | + |
| 266 | + if err != nil { |
| 267 | + panic(err) |
| 268 | + } |
| 269 | + |
| 270 | + fmt.Println(res14) // >>> [1 1] |
| 271 | + // STEP_END |
| 272 | + |
| 273 | + // Output: |
| 274 | + // 1 |
| 275 | + // 2 |
| 276 | + // 3 |
| 277 | + // 1 |
| 278 | + // 1 |
| 279 | + // 3 |
| 280 | + // [1 1] |
| 281 | +} |
0 commit comments