Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加hset hget hgetall #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dump.rdb

.idea
86 changes: 86 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ type Item struct {

ZSetValues map[string]float64

HashValues map[string]string

// Flags 一些 redis 标记位,请参考 Flag 开头的常量定义
Flags uint32

Expand Down Expand Up @@ -810,3 +812,87 @@ func (c *Client) SMembers(ctx context.Context, key string) (items [][]byte, err
})
return
}

// Hashes
func (c *Client) HSet(ctx context.Context, key string, hashes map[string]string) (added int64, err error) {
args := []interface{}{"hset", key}

for f, v := range hashes {
args = append(args, f)
args = append(args, v)
}

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
}
hashes := make(map[string] string, 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
}
hashes[string(field)] = string(value)
}
item = &Item{HashValues: hashes}
return nil
})
return
}
31 changes: 31 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,34 @@ 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, map[string]string{"name": "bilibili", "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 f, v := range hgetallItem.HashValues {
t.Logf("Key: %s, field: %s, value: %s", testKey, f, v)
}

}