Skip to content

Commit d7ce64d

Browse files
committed
Use HSET for HMSet
1 parent 2713a25 commit d7ce64d

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

commands.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ func formatSec(dur time.Duration) int64 {
3434

3535
func appendArgs(dst, src []interface{}) []interface{} {
3636
if len(src) == 1 {
37-
if ss, ok := src[0].([]string); ok {
38-
for _, s := range ss {
37+
switch v := src[0].(type) {
38+
case []string:
39+
for _, s := range v {
3940
dst = append(dst, s)
4041
}
4142
return dst
43+
case map[string]interface{}:
44+
for k, v := range v {
45+
dst = append(dst, k, v)
46+
}
47+
return dst
4248
}
4349
}
4450

@@ -107,8 +113,8 @@ type Cmdable interface {
107113
IncrBy(key string, value int64) *IntCmd
108114
IncrByFloat(key string, value float64) *FloatCmd
109115
MGet(keys ...string) *SliceCmd
110-
MSet(pairs ...interface{}) *StatusCmd
111-
MSetNX(pairs ...interface{}) *BoolCmd
116+
MSet(values ...interface{}) *StatusCmd
117+
MSetNX(values ...interface{}) *BoolCmd
112118
Set(key string, value interface{}, expiration time.Duration) *StatusCmd
113119
SetBit(key string, offset int64, value int) *IntCmd
114120
SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
@@ -124,7 +130,7 @@ type Cmdable interface {
124130
HKeys(key string) *StringSliceCmd
125131
HLen(key string) *IntCmd
126132
HMGet(key string, fields ...string) *SliceCmd
127-
HMSet(key string, fields map[string]interface{}) *StatusCmd
133+
HMSet(key string, values ...interface{}) *IntCmd
128134
HSet(key, field string, value interface{}) *BoolCmd
129135
HSetNX(key, field string, value interface{}) *BoolCmd
130136
HVals(key string) *StringSliceCmd
@@ -800,19 +806,27 @@ func (c cmdable) MGet(keys ...string) *SliceCmd {
800806
return cmd
801807
}
802808

803-
func (c cmdable) MSet(pairs ...interface{}) *StatusCmd {
804-
args := make([]interface{}, 1, 1+len(pairs))
809+
// MSet is like Set but accepts multiple values:
810+
// - MSet("key1", "value1", "key2", "value2")
811+
// - MSet([]string{"key1", "value1", "key2", "value2"})
812+
// - MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
813+
func (c cmdable) MSet(values ...interface{}) *StatusCmd {
814+
args := make([]interface{}, 1, 1+len(values))
805815
args[0] = "mset"
806-
args = appendArgs(args, pairs)
816+
args = appendArgs(args, values)
807817
cmd := NewStatusCmd(args...)
808818
_ = c(cmd)
809819
return cmd
810820
}
811821

812-
func (c cmdable) MSetNX(pairs ...interface{}) *BoolCmd {
813-
args := make([]interface{}, 1, 1+len(pairs))
822+
// MSetNX is like SetNX but accepts multiple values:
823+
// - MSetNX("key1", "value1", "key2", "value2")
824+
// - MSetNX([]string{"key1", "value1", "key2", "value2"})
825+
// - MSetNX(map[string]interface{}{"key1": "value1", "key2": "value2"})
826+
func (c cmdable) MSetNX(values ...interface{}) *BoolCmd {
827+
args := make([]interface{}, 1, 1+len(values))
814828
args[0] = "msetnx"
815-
args = appendArgs(args, pairs)
829+
args = appendArgs(args, values)
816830
cmd := NewBoolCmd(args...)
817831
_ = c(cmd)
818832
return cmd
@@ -967,17 +981,18 @@ func (c cmdable) HMGet(key string, fields ...string) *SliceCmd {
967981
return cmd
968982
}
969983

970-
func (c cmdable) HMSet(key string, fields map[string]interface{}) *StatusCmd {
971-
args := make([]interface{}, 2+len(fields)*2)
972-
args[0] = "hmset"
984+
// HMSet is like HSet, but accepts multiple values:
985+
// - HMSet("key1", "value1", "key2", "value2")
986+
// - HMSet([]string{"key1", "value1", "key2", "value2"})
987+
// - HMSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
988+
//
989+
// Note that it uses HSET Redis command underneath because HMSET is deprecated.
990+
func (c cmdable) HMSet(key string, values ...interface{}) *IntCmd {
991+
args := make([]interface{}, 2+2*len(values))
992+
args[0] = "hset"
973993
args[1] = key
974-
i := 2
975-
for k, v := range fields {
976-
args[i] = k
977-
args[i+1] = v
978-
i += 2
979-
}
980-
cmd := NewStatusCmd(args...)
994+
args = appendArgs(args, values)
995+
cmd := NewIntCmd(args...)
981996
_ = c(cmd)
982997
return cmd
983998
}

commands_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ var _ = Describe("Commands", func() {
13831383
"key2": "hello2",
13841384
}).Result()
13851385
Expect(err).NotTo(HaveOccurred())
1386-
Expect(ok).To(Equal("OK"))
1386+
Expect(ok).To(Equal(int64(3)))
13871387

13881388
v, err := client.HGet("hash", "key1").Result()
13891389
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)