mirror of https://github.com/go-redis/redis.git
Add keys method to Cmder
This commit is contained in:
parent
5b30e4ecb8
commit
c4cce2333a
13
command.go
13
command.go
|
@ -30,6 +30,9 @@ type Cmder interface {
|
||||||
// e.g. "set k v ex 10" -> "[set k v ex 10]".
|
// e.g. "set k v ex 10" -> "[set k v ex 10]".
|
||||||
Args() []interface{}
|
Args() []interface{}
|
||||||
|
|
||||||
|
//all keys in command.
|
||||||
|
Keys() []string
|
||||||
|
|
||||||
// format request and response string.
|
// format request and response string.
|
||||||
// e.g. "set k v ex 10" -> "set k v ex 10: OK", "get k" -> "get k: v".
|
// e.g. "set k v ex 10" -> "set k v ex 10: OK", "get k" -> "get k: v".
|
||||||
String() string
|
String() string
|
||||||
|
@ -119,11 +122,6 @@ func cmdString(cmd Cmder, val interface{}) string {
|
||||||
return util.BytesToString(b)
|
return util.BytesToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A wrapper around extract keys, just to make it easier to test.
|
|
||||||
func GetKeys(cmd Cmder) []string {
|
|
||||||
return extractKeys(cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func extractKeys(cmd Cmder) []string {
|
func extractKeys(cmd Cmder) []string {
|
||||||
firstKeyPos := cmdFirstKeyPos(cmd)
|
firstKeyPos := cmdFirstKeyPos(cmd)
|
||||||
if firstKeyPos == -1 {
|
if firstKeyPos == -1 {
|
||||||
|
@ -155,6 +153,7 @@ type baseCmd struct {
|
||||||
args []interface{}
|
args []interface{}
|
||||||
err error
|
err error
|
||||||
keyPos int8
|
keyPos int8
|
||||||
|
keys []string
|
||||||
rawVal interface{}
|
rawVal interface{}
|
||||||
_readTimeout *time.Duration
|
_readTimeout *time.Duration
|
||||||
}
|
}
|
||||||
|
@ -188,6 +187,10 @@ func (cmd *baseCmd) Args() []interface{} {
|
||||||
return cmd.args
|
return cmd.args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cmd *baseCmd) Keys() []string {
|
||||||
|
return cmd.keys
|
||||||
|
}
|
||||||
|
|
||||||
func (cmd *baseCmd) stringArg(pos int) string {
|
func (cmd *baseCmd) stringArg(pos int) string {
|
||||||
if pos < 0 || pos >= len(cmd.args) {
|
if pos < 0 || pos >= len(cmd.args) {
|
||||||
return ""
|
return ""
|
||||||
|
|
|
@ -7279,8 +7279,9 @@ var _ = Describe("Commands", func() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
var _ = Describe("Get Keys from Command", func() {
|
var _ = Describe("Get Keys from Commands", func() {
|
||||||
var client *redis.Client
|
var client *redis.Client
|
||||||
|
var ctx = context.TODO()
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
client = redis.NewClient(redisOptions())
|
client = redis.NewClient(redisOptions())
|
||||||
|
@ -7291,14 +7292,12 @@ var _ = Describe("Get Keys from Command", func() {
|
||||||
Expect(client.Close()).NotTo(HaveOccurred())
|
Expect(client.Close()).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns the keys extracted from the command", func() {
|
It("should test string commands", func() {
|
||||||
cmd := redis.NewCmd(ctx, "SET", "key1", "value1")
|
setCmd := client.Set(ctx, "key1", "val1", 0)
|
||||||
keys := redis.GetKeys(cmd)
|
Expect(setCmd.Keys()).To(Equal([]string{"key1"}))
|
||||||
Expect(keys).To(Equal([]string{"key1"}))
|
|
||||||
|
|
||||||
cmd = redis.NewCmd(ctx, "MGET", "key1", "key2", "key3")
|
getCmd := client.MGet(ctx, "key1", "key2", "key3")
|
||||||
keys = redis.GetKeys(cmd)
|
Expect(getCmd.Keys()).To(Equal([]string{"key1", "key2", "key3"}))
|
||||||
Expect(keys).To(Equal([]string{"key1", "key2", "key3"}))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -32,18 +32,21 @@ type StringCmdable interface {
|
||||||
|
|
||||||
func (c cmdable) Append(ctx context.Context, key, value string) *IntCmd {
|
func (c cmdable) Append(ctx context.Context, key, value string) *IntCmd {
|
||||||
cmd := NewIntCmd(ctx, "append", key, value)
|
cmd := NewIntCmd(ctx, "append", key, value)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) Decr(ctx context.Context, key string) *IntCmd {
|
func (c cmdable) Decr(ctx context.Context, key string) *IntCmd {
|
||||||
cmd := NewIntCmd(ctx, "decr", key)
|
cmd := NewIntCmd(ctx, "decr", key)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) DecrBy(ctx context.Context, key string, decrement int64) *IntCmd {
|
func (c cmdable) DecrBy(ctx context.Context, key string, decrement int64) *IntCmd {
|
||||||
cmd := NewIntCmd(ctx, "decrby", key, decrement)
|
cmd := NewIntCmd(ctx, "decrby", key, decrement)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -51,18 +54,21 @@ func (c cmdable) DecrBy(ctx context.Context, key string, decrement int64) *IntCm
|
||||||
// Get Redis `GET key` command. It returns redis.Nil error when key does not exist.
|
// Get Redis `GET key` command. It returns redis.Nil error when key does not exist.
|
||||||
func (c cmdable) Get(ctx context.Context, key string) *StringCmd {
|
func (c cmdable) Get(ctx context.Context, key string) *StringCmd {
|
||||||
cmd := NewStringCmd(ctx, "get", key)
|
cmd := NewStringCmd(ctx, "get", key)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) GetRange(ctx context.Context, key string, start, end int64) *StringCmd {
|
func (c cmdable) GetRange(ctx context.Context, key string, start, end int64) *StringCmd {
|
||||||
cmd := NewStringCmd(ctx, "getrange", key, start, end)
|
cmd := NewStringCmd(ctx, "getrange", key, start, end)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) GetSet(ctx context.Context, key string, value interface{}) *StringCmd {
|
func (c cmdable) GetSet(ctx context.Context, key string, value interface{}) *StringCmd {
|
||||||
cmd := NewStringCmd(ctx, "getset", key, value)
|
cmd := NewStringCmd(ctx, "getset", key, value)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -83,6 +89,7 @@ func (c cmdable) GetEx(ctx context.Context, key string, expiration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := NewStringCmd(ctx, args...)
|
cmd := NewStringCmd(ctx, args...)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -90,24 +97,28 @@ func (c cmdable) GetEx(ctx context.Context, key string, expiration time.Duration
|
||||||
// GetDel redis-server version >= 6.2.0.
|
// GetDel redis-server version >= 6.2.0.
|
||||||
func (c cmdable) GetDel(ctx context.Context, key string) *StringCmd {
|
func (c cmdable) GetDel(ctx context.Context, key string) *StringCmd {
|
||||||
cmd := NewStringCmd(ctx, "getdel", key)
|
cmd := NewStringCmd(ctx, "getdel", key)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) Incr(ctx context.Context, key string) *IntCmd {
|
func (c cmdable) Incr(ctx context.Context, key string) *IntCmd {
|
||||||
cmd := NewIntCmd(ctx, "incr", key)
|
cmd := NewIntCmd(ctx, "incr", key)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) IncrBy(ctx context.Context, key string, value int64) *IntCmd {
|
func (c cmdable) IncrBy(ctx context.Context, key string, value int64) *IntCmd {
|
||||||
cmd := NewIntCmd(ctx, "incrby", key, value)
|
cmd := NewIntCmd(ctx, "incrby", key, value)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) IncrByFloat(ctx context.Context, key string, value float64) *FloatCmd {
|
func (c cmdable) IncrByFloat(ctx context.Context, key string, value float64) *FloatCmd {
|
||||||
cmd := NewFloatCmd(ctx, "incrbyfloat", key, value)
|
cmd := NewFloatCmd(ctx, "incrbyfloat", key, value)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -125,6 +136,7 @@ func (c cmdable) MGet(ctx context.Context, keys ...string) *SliceCmd {
|
||||||
args[1+i] = key
|
args[1+i] = key
|
||||||
}
|
}
|
||||||
cmd := NewSliceCmd(ctx, args...)
|
cmd := NewSliceCmd(ctx, args...)
|
||||||
|
cmd.keys = append(cmd.keys, keys...)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -139,6 +151,7 @@ func (c cmdable) MSet(ctx context.Context, values ...interface{}) *StatusCmd {
|
||||||
args[0] = "mset"
|
args[0] = "mset"
|
||||||
args = appendArgs(args, values)
|
args = appendArgs(args, values)
|
||||||
cmd := NewStatusCmd(ctx, args...)
|
cmd := NewStatusCmd(ctx, args...)
|
||||||
|
// cmd.keys = append(cmd.keys, keys...)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -153,6 +166,7 @@ func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
|
||||||
args[0] = "msetnx"
|
args[0] = "msetnx"
|
||||||
args = appendArgs(args, values)
|
args = appendArgs(args, values)
|
||||||
cmd := NewBoolCmd(ctx, args...)
|
cmd := NewBoolCmd(ctx, args...)
|
||||||
|
// cmd.keys = append(cmd.keys, keys...)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -179,6 +193,7 @@ func (c cmdable) Set(ctx context.Context, key string, value interface{}, expirat
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := NewStatusCmd(ctx, args...)
|
cmd := NewStatusCmd(ctx, args...)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -230,6 +245,7 @@ func (c cmdable) SetArgs(ctx context.Context, key string, value interface{}, a S
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := NewStatusCmd(ctx, args...)
|
cmd := NewStatusCmd(ctx, args...)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -237,6 +253,7 @@ func (c cmdable) SetArgs(ctx context.Context, key string, value interface{}, a S
|
||||||
// SetEx Redis `SETEx key expiration value` command.
|
// SetEx Redis `SETEx key expiration value` command.
|
||||||
func (c cmdable) SetEx(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
|
func (c cmdable) SetEx(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
|
||||||
cmd := NewStatusCmd(ctx, "setex", key, formatSec(ctx, expiration), value)
|
cmd := NewStatusCmd(ctx, "setex", key, formatSec(ctx, expiration), value)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -261,7 +278,7 @@ func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expir
|
||||||
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "nx")
|
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "nx")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -285,19 +302,21 @@ func (c cmdable) SetXX(ctx context.Context, key string, value interface{}, expir
|
||||||
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "xx")
|
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "xx")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd {
|
func (c cmdable) SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd {
|
||||||
cmd := NewIntCmd(ctx, "setrange", key, offset, value)
|
cmd := NewIntCmd(ctx, "setrange", key, offset, value)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c cmdable) StrLen(ctx context.Context, key string) *IntCmd {
|
func (c cmdable) StrLen(ctx context.Context, key string) *IntCmd {
|
||||||
cmd := NewIntCmd(ctx, "strlen", key)
|
cmd := NewIntCmd(ctx, "strlen", key)
|
||||||
|
cmd.keys = append(cmd.keys, key)
|
||||||
_ = c(ctx, cmd)
|
_ = c(ctx, cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue