@@ -34,11 +34,17 @@ func formatSec(dur time.Duration) int64 {
34
34
35
35
func appendArgs (dst , src []interface {}) []interface {} {
36
36
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 {
39
40
dst = append (dst , s )
40
41
}
41
42
return dst
43
+ case map [string ]interface {}:
44
+ for k , v := range v {
45
+ dst = append (dst , k , v )
46
+ }
47
+ return dst
42
48
}
43
49
}
44
50
@@ -107,8 +113,8 @@ type Cmdable interface {
107
113
IncrBy (key string , value int64 ) * IntCmd
108
114
IncrByFloat (key string , value float64 ) * FloatCmd
109
115
MGet (keys ... string ) * SliceCmd
110
- MSet (pairs ... interface {}) * StatusCmd
111
- MSetNX (pairs ... interface {}) * BoolCmd
116
+ MSet (values ... interface {}) * StatusCmd
117
+ MSetNX (values ... interface {}) * BoolCmd
112
118
Set (key string , value interface {}, expiration time.Duration ) * StatusCmd
113
119
SetBit (key string , offset int64 , value int ) * IntCmd
114
120
SetNX (key string , value interface {}, expiration time.Duration ) * BoolCmd
@@ -124,7 +130,7 @@ type Cmdable interface {
124
130
HKeys (key string ) * StringSliceCmd
125
131
HLen (key string ) * IntCmd
126
132
HMGet (key string , fields ... string ) * SliceCmd
127
- HMSet (key string , fields map [ string ] interface {}) * StatusCmd
133
+ HMSet (key string , values ... interface {}) * IntCmd
128
134
HSet (key , field string , value interface {}) * BoolCmd
129
135
HSetNX (key , field string , value interface {}) * BoolCmd
130
136
HVals (key string ) * StringSliceCmd
@@ -800,19 +806,27 @@ func (c cmdable) MGet(keys ...string) *SliceCmd {
800
806
return cmd
801
807
}
802
808
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 ))
805
815
args [0 ] = "mset"
806
- args = appendArgs (args , pairs )
816
+ args = appendArgs (args , values )
807
817
cmd := NewStatusCmd (args ... )
808
818
_ = c (cmd )
809
819
return cmd
810
820
}
811
821
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 ))
814
828
args [0 ] = "msetnx"
815
- args = appendArgs (args , pairs )
829
+ args = appendArgs (args , values )
816
830
cmd := NewBoolCmd (args ... )
817
831
_ = c (cmd )
818
832
return cmd
@@ -967,17 +981,18 @@ func (c cmdable) HMGet(key string, fields ...string) *SliceCmd {
967
981
return cmd
968
982
}
969
983
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"
973
993
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 ... )
981
996
_ = c (cmd )
982
997
return cmd
983
998
}
0 commit comments