From b768284e2154ce057df69437cfd3b7ab61f17fe2 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 21 Oct 2022 14:14:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0hash=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E9=94=AE=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hash.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hash_test.go | 15 +++++++++++++ 2 files changed, 77 insertions(+) diff --git a/hash.go b/hash.go index fa2cd97..e283988 100644 --- a/hash.go +++ b/hash.go @@ -126,6 +126,35 @@ func HGetList(key string) ([]string, error) { } +//hash取所有值 + +func HGetAllItem(key string) (map[string]string, error) { + c := GetConn() + + ret, err := c.Do("HGETAll", key) + + reply := make([][]byte, 0) + + if err == nil { + reply, err = redisdb.ByteSlices(ret, err) + } + + var info map[string]string = make(map[string]string) + + if len(reply) > 0 { + for key, item := range reply { + if key%2 == 0 { //只处理奇数位 + info[string(item)] = string(reply[key+1]) + } + + } + } + + CloseConn(c) + return info, err + +} + /* * hash存值, * key 域 @@ -198,6 +227,39 @@ func HMSet(args ...interface{}) (interface{}, error) { return reply, err } +/* + * hash批量查询, + * args (key,field,field,field...) 域,名,名,名 ... + * 2020/06/06 + */ +func HMGet(args ...interface{}) (interface{}, error) { + c := GetConn() + + reply, err := c.Do("HMGET", args...) + CloseConn(c) + return reply, err +} + +/* + * hash批量查询, + * args (key,field,field,field...) 域,名,名,名 ... + * 2020/06/06 + */ +func HMGetString(args ...interface{}) ([]string, error) { + c := GetConn() + + reply, err := c.Do("HMGET", args...) + CloseConn(c) + + list := make([]string, 0) + + if err == nil { + list, err = redisdb.Strings(reply, err) + } + + return list, err +} + /* * hash存值, * key 域 diff --git a/hash_test.go b/hash_test.go index 61c1ad8..ed2b210 100644 --- a/hash_test.go +++ b/hash_test.go @@ -17,3 +17,18 @@ func Test_HVals(t *testing.T) { t.Log(err) } +func Test_HGetAllItem(t *testing.T) { + RedisPassword = "123456" + ret3, err := HGetAllItem("60007_config") + t.Log(ret3) + t.Log(err) + +} + +func Test_HMGet(t *testing.T) { + RedisPassword = "123456" + ret3, err := HMGetString("60007_config", "wx_mp_appsecret", "wx_mp_appid", "withdrawal_rate") + t.Log(ret3) + t.Log(err) + +}