mirror of https://github.com/go-redis/redis.git
Test extractKeys
This commit is contained in:
parent
249510cbb5
commit
5b30e4ecb8
11
command.go
11
command.go
|
@ -119,18 +119,23 @@ func cmdString(cmd Cmder, val interface{}) string {
|
|||
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 {
|
||||
firstKeyPos := cmdFirstKeyPos(cmd)
|
||||
if firstKeyPos == -1 {
|
||||
return nil
|
||||
}
|
||||
internal.Logger.Printf(context.Background(), "firstKeyPos: %d", firstKeyPos)
|
||||
|
||||
args := cmd.Args()
|
||||
keys := []string{}
|
||||
|
||||
switch cmd.Name() {
|
||||
case "MGET", "MSET":
|
||||
for i := int(firstKeyPos); i < len(args); i += 2 {
|
||||
case "mget":
|
||||
for i := int(firstKeyPos); i < len(args); i++ {
|
||||
keys = append(keys, cmd.stringArg(i))
|
||||
}
|
||||
|
||||
|
|
|
@ -7279,6 +7279,29 @@ var _ = Describe("Commands", func() {
|
|||
})
|
||||
})
|
||||
|
||||
var _ = Describe("Get Keys from Command", func() {
|
||||
var client *redis.Client
|
||||
|
||||
BeforeEach(func() {
|
||||
client = redis.NewClient(redisOptions())
|
||||
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
Expect(client.Close()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("returns the keys extracted from the command", func() {
|
||||
cmd := redis.NewCmd(ctx, "SET", "key1", "value1")
|
||||
keys := redis.GetKeys(cmd)
|
||||
Expect(keys).To(Equal([]string{"key1"}))
|
||||
|
||||
cmd = redis.NewCmd(ctx, "MGET", "key1", "key2", "key3")
|
||||
keys = redis.GetKeys(cmd)
|
||||
Expect(keys).To(Equal([]string{"key1", "key2", "key3"}))
|
||||
})
|
||||
})
|
||||
|
||||
type numberStruct struct {
|
||||
Number int
|
||||
}
|
||||
|
|
2
redis.go
2
redis.go
|
@ -431,7 +431,7 @@ func (c *baseClient) _process(ctx context.Context, cmd Cmder, attempt int) (bool
|
|||
return false, err
|
||||
}
|
||||
}
|
||||
internal.Logger.Printf(ctx, "redis: keys in command %s", extractKeys(cmd))
|
||||
|
||||
retryTimeout := uint32(0)
|
||||
if err := c.withConn(ctx, func(ctx context.Context, cn *pool.Conn) error {
|
||||
if err := cn.WithWriter(c.context(ctx), c.opt.WriteTimeout, func(wr *proto.Writer) error {
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -634,55 +633,3 @@ var _ = Describe("Hook with MinIdleConns", func() {
|
|||
}))
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Describe("Command Name", func() {
|
||||
var client *redis.Client
|
||||
|
||||
BeforeEach(func() {
|
||||
client = redis.NewClient(redisOptions())
|
||||
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
err := client.Close()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("should return key name", func() {
|
||||
mSet := client.MSet(ctx, "key1", "hello1", "key2", "hello2")
|
||||
Expect(mSet.Err()).NotTo(HaveOccurred())
|
||||
Expect(mSet.Val()).To(Equal("OK"))
|
||||
Expect(mSet.Args()).To(Equal([]string{"MSET", "key1", "hello1", "key2", "hello2"}))
|
||||
|
||||
mGet := client.MGet(ctx, "key1", "key2", "_")
|
||||
Expect(mGet.Err()).NotTo(HaveOccurred())
|
||||
Expect(mGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil}))
|
||||
|
||||
// MSet struct
|
||||
type set struct {
|
||||
Set1 string `redis:"set1"`
|
||||
Set2 int16 `redis:"set2"`
|
||||
Set3 time.Duration `redis:"set3"`
|
||||
Set4 interface{} `redis:"set4"`
|
||||
Set5 map[string]interface{} `redis:"-"`
|
||||
}
|
||||
mSet = client.MSet(ctx, &set{
|
||||
Set1: "val1",
|
||||
Set2: 1024,
|
||||
Set3: 2 * time.Millisecond,
|
||||
Set4: nil,
|
||||
Set5: map[string]interface{}{"k1": 1},
|
||||
})
|
||||
Expect(mSet.Err()).NotTo(HaveOccurred())
|
||||
Expect(mSet.Val()).To(Equal("OK"))
|
||||
|
||||
mGet = client.MGet(ctx, "set1", "set2", "set3", "set4")
|
||||
Expect(mGet.Err()).NotTo(HaveOccurred())
|
||||
Expect(mGet.Val()).To(Equal([]interface{}{
|
||||
"val1",
|
||||
"1024",
|
||||
strconv.Itoa(int(2 * time.Millisecond.Nanoseconds())),
|
||||
"",
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue