From 7c1a935bc67d03cb9dab204223e5d8920744c6ac Mon Sep 17 00:00:00 2001 From: satuer Date: Mon, 17 Sep 2018 15:28:39 +0800 Subject: [PATCH 1/3] HMGET command does not work because of parameter problem func Do need a interface{} slice, not a string and a []interface{}. If input k1,f1,f2, hmget get [k1, [f1, f2]], this is not work, hmget need [k1, f1, f2]. --- connection.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 53e0b04..812f522 100644 --- a/connection.go +++ b/connection.go @@ -199,7 +199,10 @@ func HGet(RConn *redigo.Conn, key string, HKey string) (interface{}, error) { } func HMGet(RConn *redigo.Conn, key string, hashKeys ...string) ([]interface{}, error) { - ret, err := (*RConn).Do(REDIS_KEYWORD_HMGET, key, hashKeys) + args := []interface{}{key} + args = append(args, hashKeys) + + ret, err := (*RConn).Do(REDIS_KEYWORD_HMGET, args...) if err != nil { return nil, err } From b70679d89b6a9de1ce28332d94ea766737a4e4db Mon Sep 17 00:00:00 2001 From: satuer Date: Mon, 17 Sep 2018 17:02:02 +0800 Subject: [PATCH 2/3] HMGET error After test, append(args, hashKeys) will return [string, []string]. So only can use for loop to get [string, string, string...] --- connection.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 812f522..79c305b 100644 --- a/connection.go +++ b/connection.go @@ -200,7 +200,9 @@ func HGet(RConn *redigo.Conn, key string, HKey string) (interface{}, error) { func HMGet(RConn *redigo.Conn, key string, hashKeys ...string) ([]interface{}, error) { args := []interface{}{key} - args = append(args, hashKeys) + for _, v := range hashKeys { + args = append(args, v) + } ret, err := (*RConn).Do(REDIS_KEYWORD_HMGET, args...) if err != nil { From 4cf5f2b1c74ba4eb3375e91418498351afc0c541 Mon Sep 17 00:00:00 2001 From: satuer Date: Mon, 17 Sep 2018 18:36:30 +0800 Subject: [PATCH 3/3] HMGET change to interface{} func HMGet1(key string, hashKeys ...string) { args := []interface{}{key} for _, v := range hashKeys { args = append(args, v) } } func HMGet2(key string, hashKeys ...string) { args := make([]interface{}, 1+len(hashKeys)) args[0] = key for i, v := range hashKeys { args[i+1] = v } } func HMGet3(key string, hashKeys ...interface{}) { args := []interface{}{key} args = append(args, hashKeys...) } 3000000 602 ns/op 3000000 417 ns/op 5000000 259 ns/op HMGet3 is best --- connection.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index 79c305b..1723b30 100644 --- a/connection.go +++ b/connection.go @@ -198,11 +198,9 @@ func HGet(RConn *redigo.Conn, key string, HKey string) (interface{}, error) { return (*RConn).Do(REDIS_KEYWORD_HGET, key, HKey) } -func HMGet(RConn *redigo.Conn, key string, hashKeys ...string) ([]interface{}, error) { +func HMGet(RConn *redigo.Conn, key string, hashKeys ...interface{}) ([]interface{}, error) { args := []interface{}{key} - for _, v := range hashKeys { - args = append(args, v) - } + args = append(args, hashKeys...) ret, err := (*RConn).Do(REDIS_KEYWORD_HMGET, args...) if err != nil {