From 2d62114dd73e6dabda36e00807cd17cd15d13f4b Mon Sep 17 00:00:00 2001 From: kaixinbaba <452914639@qq.com> Date: Mon, 4 Jan 2021 23:29:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0hset=20hget=20hgetall?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ redis.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ redis_test.go | 34 +++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/.gitignore b/.gitignore index 24ebcf5..5d4ba0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ dump.rdb + +.idea \ No newline at end of file diff --git a/redis.go b/redis.go index 0987e18..4218abc 100644 --- a/redis.go +++ b/redis.go @@ -121,6 +121,8 @@ type Item struct { ZSetValues map[string]float64 + HashValues []HashValue + // Flags 一些 redis 标记位,请参考 Flag 开头的常量定义 Flags uint32 @@ -128,6 +130,11 @@ type Item struct { TTL int32 } +type HashValue struct { + Field string + Value string +} + var noDeadline = time.Time{} // PoolStats 返回连接池状态 @@ -810,3 +817,87 @@ func (c *Client) SMembers(ctx context.Context, key string) (items [][]byte, err }) return } + +// Hashes +func (c *Client) HSet(ctx context.Context, key string, hashValues ...HashValue) (added int64, err error) { + args := []interface{}{"hset", key} + + for _, h := range hashValues { + args = append(args, h.Field) + args = append(args, h.Value) + } + + err = c.do(ctx, args, func(conn *redisConn) error { + if err := conn.w.WriteArgs(args); err != nil { + return err + } + + if err := conn.w.Flush(); err != nil { + return err + } + + added, err = conn.r.ReadIntReply() + if err != nil { + return err + } + return nil + }) + return +} + +func (c *Client) HGet(ctx context.Context, key string, field string) (item *Item, err error) { + args := []interface{}{"hget", key, field} + err = c.do(ctx, args, func(conn *redisConn) error { + if err := conn.w.WriteArgs(args); err != nil { + return err + } + + if err := conn.w.Flush(); err != nil { + return err + } + + var b []byte + if b, err = conn.r.ReadBytesReply(); err != nil { + item = nil + return err + } + + item = &Item{Value: b} + + return nil + }) + return +} + +func (c *Client) HGetAll(ctx context.Context, key string) (item *Item, err error) { + args := []interface{}{"hgetall", key} + err = c.do(ctx, args, func(conn *redisConn) error { + if err := conn.w.WriteArgs(args); err != nil { + return err + } + + if err := conn.w.Flush(); err != nil { + return err + } + + l, err := conn.r.ReadArrayLenReply() + if err != nil { + return err + } + hashValues := make([]HashValue, l/2) + for i := 0; i < l; i += 2 { + field, err := conn.r.ReadBytesReply() + if err != nil { + return err + } + value, err := conn.r.ReadBytesReply() + if err != nil { + return err + } + hashValues[i/2] = HashValue{Field: string(field), Value: string(value)} + } + item = &Item{HashValues: hashValues} + return nil + }) + return +} diff --git a/redis_test.go b/redis_test.go index 5013127..4130366 100644 --- a/redis_test.go +++ b/redis_test.go @@ -354,3 +354,37 @@ func TestSet(t *testing.T) { t.Logf("Key: foo, SRem: %d; Failed, err: %v", result, err) } } + + +func TestHash(t *testing.T) { + c := New(Options{ + Address: os.Getenv("REDIS_HOST"), + PoolSize: 1, + }) + + testKey := "hashtest" + + if err := c.Del(ctx, testKey); err != nil { + t.Fatal("start faild") + } + + + + // hset + added, _ := c.HSet(ctx, testKey, HashValue{"name", "bilibili"}, HashValue{"age", "20"}) + if added != 2 { + t.Fatalf("hset %s failed", testKey) + } + // hget + item, _ := c.HGet(ctx, testKey, "name") + if !bytes.Equal(item.Value, []byte("bilibili")) { + t.Fatalf("hget %s failed", testKey) + } + + // hgetall + hgetallItem, _ := c.HGetAll(ctx, testKey) + for _, hashValue := range hgetallItem.HashValues { + t.Logf("Key: %s, field: %s, value: %s", testKey, hashValue.Field, hashValue.Value) + } + +} \ No newline at end of file From 4100a9cafdbce491fbcd88c280ecd3ab19b150f4 Mon Sep 17 00:00:00 2001 From: kaixinbaba <452914639@qq.com> Date: Wed, 6 Jan 2021 18:05:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9Item=20HashValues=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=B8=BA=20map[string]string=20=E5=8F=8A?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.go | 21 ++++++++------------- redis_test.go | 11 ++++------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/redis.go b/redis.go index 4218abc..d7a48e6 100644 --- a/redis.go +++ b/redis.go @@ -121,7 +121,7 @@ type Item struct { ZSetValues map[string]float64 - HashValues []HashValue + HashValues map[string]string // Flags 一些 redis 标记位,请参考 Flag 开头的常量定义 Flags uint32 @@ -130,11 +130,6 @@ type Item struct { TTL int32 } -type HashValue struct { - Field string - Value string -} - var noDeadline = time.Time{} // PoolStats 返回连接池状态 @@ -819,12 +814,12 @@ func (c *Client) SMembers(ctx context.Context, key string) (items [][]byte, err } // Hashes -func (c *Client) HSet(ctx context.Context, key string, hashValues ...HashValue) (added int64, err error) { +func (c *Client) HSet(ctx context.Context, key string, hashes map[string]string) (added int64, err error) { args := []interface{}{"hset", key} - for _, h := range hashValues { - args = append(args, h.Field) - args = append(args, h.Value) + for f, v := range hashes { + args = append(args, f) + args = append(args, v) } err = c.do(ctx, args, func(conn *redisConn) error { @@ -884,7 +879,7 @@ func (c *Client) HGetAll(ctx context.Context, key string) (item *Item, err error if err != nil { return err } - hashValues := make([]HashValue, l/2) + hashes := make(map[string] string, l / 2) for i := 0; i < l; i += 2 { field, err := conn.r.ReadBytesReply() if err != nil { @@ -894,9 +889,9 @@ func (c *Client) HGetAll(ctx context.Context, key string) (item *Item, err error if err != nil { return err } - hashValues[i/2] = HashValue{Field: string(field), Value: string(value)} + hashes[string(field)] = string(value) } - item = &Item{HashValues: hashValues} + item = &Item{HashValues: hashes} return nil }) return diff --git a/redis_test.go b/redis_test.go index 4130366..ad313ed 100644 --- a/redis_test.go +++ b/redis_test.go @@ -355,7 +355,6 @@ func TestSet(t *testing.T) { } } - func TestHash(t *testing.T) { c := New(Options{ Address: os.Getenv("REDIS_HOST"), @@ -368,10 +367,8 @@ func TestHash(t *testing.T) { t.Fatal("start faild") } - - // hset - added, _ := c.HSet(ctx, testKey, HashValue{"name", "bilibili"}, HashValue{"age", "20"}) + added, _ := c.HSet(ctx, testKey, map[string]string{"name": "bilibili", "age": "20"}) if added != 2 { t.Fatalf("hset %s failed", testKey) } @@ -383,8 +380,8 @@ func TestHash(t *testing.T) { // hgetall hgetallItem, _ := c.HGetAll(ctx, testKey) - for _, hashValue := range hgetallItem.HashValues { - t.Logf("Key: %s, field: %s, value: %s", testKey, hashValue.Field, hashValue.Value) + for f, v := range hgetallItem.HashValues { + t.Logf("Key: %s, field: %s, value: %s", testKey, f, v) } -} \ No newline at end of file +}