Skip to content

Commit c4cce23

Browse files
committed
Add keys method to Cmder
1 parent 5b30e4e commit c4cce23

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

command.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ type Cmder interface {
3030
// e.g. "set k v ex 10" -> "[set k v ex 10]".
3131
Args() []interface{}
3232

33+
//all keys in command.
34+
Keys() []string
35+
3336
// format request and response string.
3437
// e.g. "set k v ex 10" -> "set k v ex 10: OK", "get k" -> "get k: v".
3538
String() string
@@ -119,11 +122,6 @@ func cmdString(cmd Cmder, val interface{}) string {
119122
return util.BytesToString(b)
120123
}
121124

122-
// A wrapper around extract keys, just to make it easier to test.
123-
func GetKeys(cmd Cmder) []string {
124-
return extractKeys(cmd)
125-
}
126-
127125
func extractKeys(cmd Cmder) []string {
128126
firstKeyPos := cmdFirstKeyPos(cmd)
129127
if firstKeyPos == -1 {
@@ -155,6 +153,7 @@ type baseCmd struct {
155153
args []interface{}
156154
err error
157155
keyPos int8
156+
keys []string
158157
rawVal interface{}
159158
_readTimeout *time.Duration
160159
}
@@ -188,6 +187,10 @@ func (cmd *baseCmd) Args() []interface{} {
188187
return cmd.args
189188
}
190189

190+
func (cmd *baseCmd) Keys() []string {
191+
return cmd.keys
192+
}
193+
191194
func (cmd *baseCmd) stringArg(pos int) string {
192195
if pos < 0 || pos >= len(cmd.args) {
193196
return ""

commands_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7279,8 +7279,9 @@ var _ = Describe("Commands", func() {
72797279
})
72807280
})
72817281

7282-
var _ = Describe("Get Keys from Command", func() {
7282+
var _ = Describe("Get Keys from Commands", func() {
72837283
var client *redis.Client
7284+
var ctx = context.TODO()
72847285

72857286
BeforeEach(func() {
72867287
client = redis.NewClient(redisOptions())
@@ -7291,14 +7292,12 @@ var _ = Describe("Get Keys from Command", func() {
72917292
Expect(client.Close()).NotTo(HaveOccurred())
72927293
})
72937294

7294-
It("returns the keys extracted from the command", func() {
7295-
cmd := redis.NewCmd(ctx, "SET", "key1", "value1")
7296-
keys := redis.GetKeys(cmd)
7297-
Expect(keys).To(Equal([]string{"key1"}))
7295+
It("should test string commands", func() {
7296+
setCmd := client.Set(ctx, "key1", "val1", 0)
7297+
Expect(setCmd.Keys()).To(Equal([]string{"key1"}))
72987298

7299-
cmd = redis.NewCmd(ctx, "MGET", "key1", "key2", "key3")
7300-
keys = redis.GetKeys(cmd)
7301-
Expect(keys).To(Equal([]string{"key1", "key2", "key3"}))
7299+
getCmd := client.MGet(ctx, "key1", "key2", "key3")
7300+
Expect(getCmd.Keys()).To(Equal([]string{"key1", "key2", "key3"}))
73027301
})
73037302
})
73047303

string_commands.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,43 @@ type StringCmdable interface {
3232

3333
func (c cmdable) Append(ctx context.Context, key, value string) *IntCmd {
3434
cmd := NewIntCmd(ctx, "append", key, value)
35+
cmd.keys = append(cmd.keys, key)
3536
_ = c(ctx, cmd)
3637
return cmd
3738
}
3839

3940
func (c cmdable) Decr(ctx context.Context, key string) *IntCmd {
4041
cmd := NewIntCmd(ctx, "decr", key)
42+
cmd.keys = append(cmd.keys, key)
4143
_ = c(ctx, cmd)
4244
return cmd
4345
}
4446

4547
func (c cmdable) DecrBy(ctx context.Context, key string, decrement int64) *IntCmd {
4648
cmd := NewIntCmd(ctx, "decrby", key, decrement)
49+
cmd.keys = append(cmd.keys, key)
4750
_ = c(ctx, cmd)
4851
return cmd
4952
}
5053

5154
// Get Redis `GET key` command. It returns redis.Nil error when key does not exist.
5255
func (c cmdable) Get(ctx context.Context, key string) *StringCmd {
5356
cmd := NewStringCmd(ctx, "get", key)
57+
cmd.keys = append(cmd.keys, key)
5458
_ = c(ctx, cmd)
5559
return cmd
5660
}
5761

5862
func (c cmdable) GetRange(ctx context.Context, key string, start, end int64) *StringCmd {
5963
cmd := NewStringCmd(ctx, "getrange", key, start, end)
64+
cmd.keys = append(cmd.keys, key)
6065
_ = c(ctx, cmd)
6166
return cmd
6267
}
6368

6469
func (c cmdable) GetSet(ctx context.Context, key string, value interface{}) *StringCmd {
6570
cmd := NewStringCmd(ctx, "getset", key, value)
71+
cmd.keys = append(cmd.keys, key)
6672
_ = c(ctx, cmd)
6773
return cmd
6874
}
@@ -83,31 +89,36 @@ func (c cmdable) GetEx(ctx context.Context, key string, expiration time.Duration
8389
}
8490

8591
cmd := NewStringCmd(ctx, args...)
92+
cmd.keys = append(cmd.keys, key)
8693
_ = c(ctx, cmd)
8794
return cmd
8895
}
8996

9097
// GetDel redis-server version >= 6.2.0.
9198
func (c cmdable) GetDel(ctx context.Context, key string) *StringCmd {
9299
cmd := NewStringCmd(ctx, "getdel", key)
100+
cmd.keys = append(cmd.keys, key)
93101
_ = c(ctx, cmd)
94102
return cmd
95103
}
96104

97105
func (c cmdable) Incr(ctx context.Context, key string) *IntCmd {
98106
cmd := NewIntCmd(ctx, "incr", key)
107+
cmd.keys = append(cmd.keys, key)
99108
_ = c(ctx, cmd)
100109
return cmd
101110
}
102111

103112
func (c cmdable) IncrBy(ctx context.Context, key string, value int64) *IntCmd {
104113
cmd := NewIntCmd(ctx, "incrby", key, value)
114+
cmd.keys = append(cmd.keys, key)
105115
_ = c(ctx, cmd)
106116
return cmd
107117
}
108118

109119
func (c cmdable) IncrByFloat(ctx context.Context, key string, value float64) *FloatCmd {
110120
cmd := NewFloatCmd(ctx, "incrbyfloat", key, value)
121+
cmd.keys = append(cmd.keys, key)
111122
_ = c(ctx, cmd)
112123
return cmd
113124
}
@@ -125,6 +136,7 @@ func (c cmdable) MGet(ctx context.Context, keys ...string) *SliceCmd {
125136
args[1+i] = key
126137
}
127138
cmd := NewSliceCmd(ctx, args...)
139+
cmd.keys = append(cmd.keys, keys...)
128140
_ = c(ctx, cmd)
129141
return cmd
130142
}
@@ -139,6 +151,7 @@ func (c cmdable) MSet(ctx context.Context, values ...interface{}) *StatusCmd {
139151
args[0] = "mset"
140152
args = appendArgs(args, values)
141153
cmd := NewStatusCmd(ctx, args...)
154+
// cmd.keys = append(cmd.keys, keys...)
142155
_ = c(ctx, cmd)
143156
return cmd
144157
}
@@ -153,6 +166,7 @@ func (c cmdable) MSetNX(ctx context.Context, values ...interface{}) *BoolCmd {
153166
args[0] = "msetnx"
154167
args = appendArgs(args, values)
155168
cmd := NewBoolCmd(ctx, args...)
169+
// cmd.keys = append(cmd.keys, keys...)
156170
_ = c(ctx, cmd)
157171
return cmd
158172
}
@@ -179,6 +193,7 @@ func (c cmdable) Set(ctx context.Context, key string, value interface{}, expirat
179193
}
180194

181195
cmd := NewStatusCmd(ctx, args...)
196+
cmd.keys = append(cmd.keys, key)
182197
_ = c(ctx, cmd)
183198
return cmd
184199
}
@@ -230,13 +245,15 @@ func (c cmdable) SetArgs(ctx context.Context, key string, value interface{}, a S
230245
}
231246

232247
cmd := NewStatusCmd(ctx, args...)
248+
cmd.keys = append(cmd.keys, key)
233249
_ = c(ctx, cmd)
234250
return cmd
235251
}
236252

237253
// SetEx Redis `SETEx key expiration value` command.
238254
func (c cmdable) SetEx(ctx context.Context, key string, value interface{}, expiration time.Duration) *StatusCmd {
239255
cmd := NewStatusCmd(ctx, "setex", key, formatSec(ctx, expiration), value)
256+
cmd.keys = append(cmd.keys, key)
240257
_ = c(ctx, cmd)
241258
return cmd
242259
}
@@ -261,7 +278,7 @@ func (c cmdable) SetNX(ctx context.Context, key string, value interface{}, expir
261278
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "nx")
262279
}
263280
}
264-
281+
cmd.keys = append(cmd.keys, key)
265282
_ = c(ctx, cmd)
266283
return cmd
267284
}
@@ -285,19 +302,21 @@ func (c cmdable) SetXX(ctx context.Context, key string, value interface{}, expir
285302
cmd = NewBoolCmd(ctx, "set", key, value, "ex", formatSec(ctx, expiration), "xx")
286303
}
287304
}
288-
305+
cmd.keys = append(cmd.keys, key)
289306
_ = c(ctx, cmd)
290307
return cmd
291308
}
292309

293310
func (c cmdable) SetRange(ctx context.Context, key string, offset int64, value string) *IntCmd {
294311
cmd := NewIntCmd(ctx, "setrange", key, offset, value)
312+
cmd.keys = append(cmd.keys, key)
295313
_ = c(ctx, cmd)
296314
return cmd
297315
}
298316

299317
func (c cmdable) StrLen(ctx context.Context, key string) *IntCmd {
300318
cmd := NewIntCmd(ctx, "strlen", key)
319+
cmd.keys = append(cmd.keys, key)
301320
_ = c(ctx, cmd)
302321
return cmd
303322
}

0 commit comments

Comments
 (0)