mirror of https://github.com/go-redis/redis.git
Use HSET for HMSet
This commit is contained in:
parent
2713a259c5
commit
d7ce64d287
57
commands.go
57
commands.go
|
@ -34,11 +34,17 @@ func formatSec(dur time.Duration) int64 {
|
||||||
|
|
||||||
func appendArgs(dst, src []interface{}) []interface{} {
|
func appendArgs(dst, src []interface{}) []interface{} {
|
||||||
if len(src) == 1 {
|
if len(src) == 1 {
|
||||||
if ss, ok := src[0].([]string); ok {
|
switch v := src[0].(type) {
|
||||||
for _, s := range ss {
|
case []string:
|
||||||
|
for _, s := range v {
|
||||||
dst = append(dst, s)
|
dst = append(dst, s)
|
||||||
}
|
}
|
||||||
return dst
|
return dst
|
||||||
|
case map[string]interface{}:
|
||||||
|
for k, v := range v {
|
||||||
|
dst = append(dst, k, v)
|
||||||
|
}
|
||||||
|
return dst
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,8 +113,8 @@ type Cmdable interface {
|
||||||
IncrBy(key string, value int64) *IntCmd
|
IncrBy(key string, value int64) *IntCmd
|
||||||
IncrByFloat(key string, value float64) *FloatCmd
|
IncrByFloat(key string, value float64) *FloatCmd
|
||||||
MGet(keys ...string) *SliceCmd
|
MGet(keys ...string) *SliceCmd
|
||||||
MSet(pairs ...interface{}) *StatusCmd
|
MSet(values ...interface{}) *StatusCmd
|
||||||
MSetNX(pairs ...interface{}) *BoolCmd
|
MSetNX(values ...interface{}) *BoolCmd
|
||||||
Set(key string, value interface{}, expiration time.Duration) *StatusCmd
|
Set(key string, value interface{}, expiration time.Duration) *StatusCmd
|
||||||
SetBit(key string, offset int64, value int) *IntCmd
|
SetBit(key string, offset int64, value int) *IntCmd
|
||||||
SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
|
SetNX(key string, value interface{}, expiration time.Duration) *BoolCmd
|
||||||
|
@ -124,7 +130,7 @@ type Cmdable interface {
|
||||||
HKeys(key string) *StringSliceCmd
|
HKeys(key string) *StringSliceCmd
|
||||||
HLen(key string) *IntCmd
|
HLen(key string) *IntCmd
|
||||||
HMGet(key string, fields ...string) *SliceCmd
|
HMGet(key string, fields ...string) *SliceCmd
|
||||||
HMSet(key string, fields map[string]interface{}) *StatusCmd
|
HMSet(key string, values ...interface{}) *IntCmd
|
||||||
HSet(key, field string, value interface{}) *BoolCmd
|
HSet(key, field string, value interface{}) *BoolCmd
|
||||||
HSetNX(key, field string, value interface{}) *BoolCmd
|
HSetNX(key, field string, value interface{}) *BoolCmd
|
||||||
HVals(key string) *StringSliceCmd
|
HVals(key string) *StringSliceCmd
|
||||||
|
@ -800,19 +806,27 @@ func (c cmdable) MGet(keys ...string) *SliceCmd {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) MSet(pairs ...interface{}) *StatusCmd {
|
// MSet is like Set but accepts multiple values:
|
||||||
args := make([]interface{}, 1, 1+len(pairs))
|
// - MSet("key1", "value1", "key2", "value2")
|
||||||
|
// - MSet([]string{"key1", "value1", "key2", "value2"})
|
||||||
|
// - MSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
|
||||||
|
func (c cmdable) MSet(values ...interface{}) *StatusCmd {
|
||||||
|
args := make([]interface{}, 1, 1+len(values))
|
||||||
args[0] = "mset"
|
args[0] = "mset"
|
||||||
args = appendArgs(args, pairs)
|
args = appendArgs(args, values)
|
||||||
cmd := NewStatusCmd(args...)
|
cmd := NewStatusCmd(args...)
|
||||||
_ = c(cmd)
|
_ = c(cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) MSetNX(pairs ...interface{}) *BoolCmd {
|
// MSetNX is like SetNX but accepts multiple values:
|
||||||
args := make([]interface{}, 1, 1+len(pairs))
|
// - MSetNX("key1", "value1", "key2", "value2")
|
||||||
|
// - MSetNX([]string{"key1", "value1", "key2", "value2"})
|
||||||
|
// - MSetNX(map[string]interface{}{"key1": "value1", "key2": "value2"})
|
||||||
|
func (c cmdable) MSetNX(values ...interface{}) *BoolCmd {
|
||||||
|
args := make([]interface{}, 1, 1+len(values))
|
||||||
args[0] = "msetnx"
|
args[0] = "msetnx"
|
||||||
args = appendArgs(args, pairs)
|
args = appendArgs(args, values)
|
||||||
cmd := NewBoolCmd(args...)
|
cmd := NewBoolCmd(args...)
|
||||||
_ = c(cmd)
|
_ = c(cmd)
|
||||||
return cmd
|
return cmd
|
||||||
|
@ -967,17 +981,18 @@ func (c cmdable) HMGet(key string, fields ...string) *SliceCmd {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) HMSet(key string, fields map[string]interface{}) *StatusCmd {
|
// HMSet is like HSet, but accepts multiple values:
|
||||||
args := make([]interface{}, 2+len(fields)*2)
|
// - HMSet("key1", "value1", "key2", "value2")
|
||||||
args[0] = "hmset"
|
// - HMSet([]string{"key1", "value1", "key2", "value2"})
|
||||||
|
// - HMSet(map[string]interface{}{"key1": "value1", "key2": "value2"})
|
||||||
|
//
|
||||||
|
// Note that it uses HSET Redis command underneath because HMSET is deprecated.
|
||||||
|
func (c cmdable) HMSet(key string, values ...interface{}) *IntCmd {
|
||||||
|
args := make([]interface{}, 2+2*len(values))
|
||||||
|
args[0] = "hset"
|
||||||
args[1] = key
|
args[1] = key
|
||||||
i := 2
|
args = appendArgs(args, values)
|
||||||
for k, v := range fields {
|
cmd := NewIntCmd(args...)
|
||||||
args[i] = k
|
|
||||||
args[i+1] = v
|
|
||||||
i += 2
|
|
||||||
}
|
|
||||||
cmd := NewStatusCmd(args...)
|
|
||||||
_ = c(cmd)
|
_ = c(cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -1383,7 +1383,7 @@ var _ = Describe("Commands", func() {
|
||||||
"key2": "hello2",
|
"key2": "hello2",
|
||||||
}).Result()
|
}).Result()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(ok).To(Equal("OK"))
|
Expect(ok).To(Equal(int64(3)))
|
||||||
|
|
||||||
v, err := client.HGet("hash", "key1").Result()
|
v, err := client.HGet("hash", "key1").Result()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
Loading…
Reference in New Issue