mirror of
https://github.com/hanzokv/go.git
synced 2026-09-24 00:31:51 +00:00
We do not use Redis. The client was a go-redis fork and still said so
everywhere: `package redis`, `redis.NewClient`, `redis://`, `extra/redisotel`.
Consumers wrote `redis.Client` against a Valkey-backed store called KV.
package redis -> package kv (93 decls)
redis.X -> kv.X (2,794 call sites)
"redis: …" errors -> "kv: …" (206)
extra/redis{otel,cmd,prometheus,census} -> extra/kv{otel,cmd,prometheus,census}
redis:// , rediss:// -> kv:// , kvs:// (ParseURL)
Redis*/*Redis* idents -> KV*/*KV*
REDIS_VERSION, REDIS_ENDPOINTS_CONFIG_PATH -> KV_*
I previously argued this rename was unsafe because much of it was "wire
contract". That was wrong, and the categorisation shows why: RESP command names
are GET/SET/HSET — they never contain the word. Of 5,771 occurrences only two
kinds were server-visible at all: the ParseURL scheme (we choose what we pass;
our configs already use kv://) and the CLIENT SETINFO lib-name label, which the
server merely records. Nothing here changes a byte of protocol.
Two things deliberately keep the old spelling:
RediSearch — names a specific external module the FT.* commands map to.
StoreDist — contains the letters by accident (sto-reDis-t), not the word.
Breaking for consumers by design: every import site must move redis.X -> kv.X.
Minor bump, never a major — this module stays v9 forever.
290 lines
5.0 KiB
Go
290 lines
5.0 KiB
Go
// EXAMPLE: hash_tutorial
|
|
// HIDE_START
|
|
package example_commands_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hanzokv/go/v9"
|
|
)
|
|
|
|
// HIDE_END
|
|
|
|
func ExampleClient_set_get_all() {
|
|
ctx := context.Background()
|
|
|
|
rdb := kv.NewClient(&kv.Options{
|
|
Addr: "localhost:6379",
|
|
Password: "", // no password docs
|
|
DB: 0, // use default DB
|
|
})
|
|
|
|
// REMOVE_START
|
|
// make sure we are working with fresh database
|
|
rdb.FlushDB(ctx)
|
|
rdb.Del(ctx, "bike:1")
|
|
// REMOVE_END
|
|
|
|
// STEP_START set_get_all
|
|
hashFields := []string{
|
|
"model", "Deimos",
|
|
"brand", "Ergonom",
|
|
"type", "Enduro bikes",
|
|
"price", "4972",
|
|
}
|
|
|
|
res1, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res1) // >>> 4
|
|
|
|
res2, err := rdb.HGet(ctx, "bike:1", "model").Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res2) // >>> Deimos
|
|
|
|
res3, err := rdb.HGet(ctx, "bike:1", "price").Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res3) // >>> 4972
|
|
|
|
cmdReturn := rdb.HGetAll(ctx, "bike:1")
|
|
res4, err := cmdReturn.Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res4)
|
|
// >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]
|
|
|
|
type BikeInfo struct {
|
|
Model string `kv:"model"`
|
|
Brand string `kv:"brand"`
|
|
Type string `kv:"type"`
|
|
Price int `kv:"price"`
|
|
}
|
|
|
|
var res4a BikeInfo
|
|
|
|
if err := cmdReturn.Scan(&res4a); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("Model: %v, Brand: %v, Type: %v, Price: $%v\n",
|
|
res4a.Model, res4a.Brand, res4a.Type, res4a.Price)
|
|
// >>> Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972
|
|
// STEP_END
|
|
|
|
// Output:
|
|
// 4
|
|
// Deimos
|
|
// 4972
|
|
// map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]
|
|
// Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972
|
|
}
|
|
|
|
func ExampleClient_hmget() {
|
|
ctx := context.Background()
|
|
|
|
rdb := kv.NewClient(&kv.Options{
|
|
Addr: "localhost:6379",
|
|
Password: "", // no password docs
|
|
DB: 0, // use default DB
|
|
})
|
|
|
|
// REMOVE_START
|
|
// start with fresh database
|
|
rdb.FlushDB(ctx)
|
|
rdb.Del(ctx, "bike:1")
|
|
// REMOVE_END
|
|
|
|
hashFields := []string{
|
|
"model", "Deimos",
|
|
"brand", "Ergonom",
|
|
"type", "Enduro bikes",
|
|
"price", "4972",
|
|
}
|
|
|
|
_, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// STEP_START hmget
|
|
cmdReturn := rdb.HMGet(ctx, "bike:1", "model", "price")
|
|
res5, err := cmdReturn.Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res5) // >>> [Deimos 4972]
|
|
|
|
type BikeInfo struct {
|
|
Model string `kv:"model"`
|
|
Brand string `kv:"-"`
|
|
Type string `kv:"-"`
|
|
Price int `kv:"price"`
|
|
}
|
|
|
|
var res5a BikeInfo
|
|
|
|
if err := cmdReturn.Scan(&res5a); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("Model: %v, Price: $%v\n", res5a.Model, res5a.Price)
|
|
// >>> Model: Deimos, Price: $4972
|
|
// STEP_END
|
|
|
|
// Output:
|
|
// [Deimos 4972]
|
|
// Model: Deimos, Price: $4972
|
|
}
|
|
|
|
func ExampleClient_hincrby() {
|
|
ctx := context.Background()
|
|
|
|
rdb := kv.NewClient(&kv.Options{
|
|
Addr: "localhost:6379",
|
|
Password: "", // no password docs
|
|
DB: 0, // use default DB
|
|
})
|
|
|
|
// REMOVE_START
|
|
// start with fresh database
|
|
rdb.FlushDB(ctx)
|
|
rdb.Del(ctx, "bike:1")
|
|
// REMOVE_END
|
|
|
|
hashFields := []string{
|
|
"model", "Deimos",
|
|
"brand", "Ergonom",
|
|
"type", "Enduro bikes",
|
|
"price", "4972",
|
|
}
|
|
|
|
_, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// STEP_START hincrby
|
|
res6, err := rdb.HIncrBy(ctx, "bike:1", "price", 100).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res6) // >>> 5072
|
|
|
|
res7, err := rdb.HIncrBy(ctx, "bike:1", "price", -100).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res7) // >>> 4972
|
|
// STEP_END
|
|
|
|
// Output:
|
|
// 5072
|
|
// 4972
|
|
}
|
|
|
|
func ExampleClient_incrby_get_mget() {
|
|
ctx := context.Background()
|
|
|
|
rdb := kv.NewClient(&kv.Options{
|
|
Addr: "localhost:6379",
|
|
Password: "", // no password docs
|
|
DB: 0, // use default DB
|
|
})
|
|
|
|
// REMOVE_START
|
|
// start with fresh database
|
|
rdb.FlushDB(ctx)
|
|
rdb.Del(ctx, "bike:1:stats")
|
|
// REMOVE_END
|
|
|
|
// STEP_START incrby_get_mget
|
|
res8, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res8) // >>> 1
|
|
|
|
res9, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res9) // >>> 2
|
|
|
|
res10, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res10) // >>> 3
|
|
|
|
res11, err := rdb.HIncrBy(ctx, "bike:1:stats", "crashes", 1).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res11) // >>> 1
|
|
|
|
res12, err := rdb.HIncrBy(ctx, "bike:1:stats", "owners", 1).Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res12) // >>> 1
|
|
|
|
res13, err := rdb.HGet(ctx, "bike:1:stats", "rides").Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res13) // >>> 3
|
|
|
|
res14, err := rdb.HMGet(ctx, "bike:1:stats", "crashes", "owners").Result()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Println(res14) // >>> [1 1]
|
|
// STEP_END
|
|
|
|
// Output:
|
|
// 1
|
|
// 2
|
|
// 3
|
|
// 1
|
|
// 1
|
|
// 3
|
|
// [1 1]
|
|
}
|