Skip to content

Conversation

@yxyx8
Copy link
Contributor

@yxyx8 yxyx8 commented Sep 17, 2018

After test, append(args, hashKeys) will return [string, []string]. So only can use ‘for loop‘ to get [string, string, string...], then hmget can get the well return

satuer added 2 commits September 17, 2018 15:28
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].
After test, append(args, hashKeys) will return [string, []string]. So only can use for loop to get [string, string, string...]
connection.go Outdated

func HMGet(RConn *redigo.Conn, key string, hashKeys ...string) ([]interface{}, error) {
args := []interface{}{key}
args = append(args, hashKeys)
Copy link
Owner

@yadvendar yadvendar Sep 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried this? - args = append(args, hasKeys...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course, but cannot use hasKeys(type []string) as type []interface{}, unless change the hasKeys to ...interface{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

I bench test the three method make a string and []string to a []string. The HMGet3 is best, so i suggest modifying the function parameter type to ...interface{}.
like this
func HMGet(RConn *redigo.Conn, key string, hashKeys ...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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants