-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_list.go
534 lines (482 loc) · 21.4 KB
/
cmd_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package redisson
import (
"context"
"fmt"
"strings"
"time"
)
type ListCmdable interface {
ListWriter
ListReader
}
type ListWriter interface {
// BLMove
// Available since: 6.2.0
// Time complexity: O(1)
// ACL categories: @write @list @slow @blocking
// RESP2 Reply:
// One of the following:
// - Bulk string reply: the element being popped from the source and pushed to the destination.
// - Nil reply: the operation timed-out
// RESP3 Reply:
// One of the following:
// - Bulk string reply: the element being popped from the source and pushed to the destination.
// - Null reply: the operation timed-out
BLMove(ctx context.Context, source, destination, srcpos, destpos string, timeout time.Duration) StringCmd
// BLMPop
// Available since: 7.0.0
// Time complexity: O(N+M) where N is the number of provided keys and M is the number of elements returned.
// ACL categories: @write, @list, @slow, @blocking
// RESP2 Reply:
// One of the following:
// - Nil reply: when no element could be popped and the timeout is reached.
// - Array reply: a two-element array with the first element being the name of the key from which elements were popped, and the second element being an array of the popped elements.
// RESP3 Reply:
// One of the following:
// - Null reply: when no element could be popped and the timeout is reached.
// - Array reply: a two-element array with the first element being the name of the key from which elements were popped, and the second element being an array of the popped elements.
BLMPop(ctx context.Context, timeout time.Duration, direction string, count int64, keys ...string) KeyValuesCmd
// BLPop
// Available since: 2.0.0
// Time complexity: O(N) where N is the number of provided keys.
// ACL categories: @write @list @slow @blocking
// RESP2 Reply:
// One of the following:
// - Nil reply: no element could be popped and the timeout expired
// - Array reply: the key from which the element was popped and the value of the popped element.
// RESP3 Reply:
// One of the following:
// - Null reply: no element could be popped and the timeout expired
// - Array reply: the key from which the element was popped and the value of the popped element.
// History:
// - Starting with Redis version 6.0.0: timeout is interpreted as a double instead of an integer.
BLPop(ctx context.Context, timeout time.Duration, keys ...string) StringSliceCmd
// BRPop
// Available since: 2.0.0
// Time complexity: O(N) where N is the number of provided keys.
// ACL categories: @write @list @slow @blocking
// RESP2 Reply:
// One of the following:
// - Nil reply: no element could be popped and the timeout expired.
// - Array reply: the key from which the element was popped and the value of the popped element
// RESP3 Reply:
// One of the following:
// - Null reply: no element could be popped and the timeout expired.
// - Array reply: the key from which the element was popped and the value of the popped element
// History:
// - Starting with Redis version 6.0.0: timeout is interpreted as a double instead of an integer.
BRPop(ctx context.Context, timeout time.Duration, keys ...string) StringSliceCmd
// BRPopLPush
// Available since: 2.2.0
// Time complexity: O(1)
// ACL categories: @write @list @slow @blocking
// RESP2 Reply:
// One of the following:
// - Bulk string reply: the element being popped from source and pushed to destination.
// - Nil reply: the timeout is reached.
// RESP3 Reply:
// One of the following:
// - Bulk string reply: the element being popped from source and pushed to destination.
// - Null reply: the timeout is reached.
// History:
// - Starting with Redis version 6.0.0: timeout is interpreted as a double instead of an integer.
BRPopLPush(ctx context.Context, source, destination string, timeout time.Duration) StringCmd
// LInsert
// Available since: 2.2.0
// Time complexity: O(N) where N is the number of elements to traverse before seeing the value pivot.
// This means that inserting somewhere on the left end on the list (head) can be considered O(1) and inserting somewhere on the right end (tail) is O(N).
// ACL categories: @write @list @slow
// RESP2 / RESP3 Reply:
// One of the following:
// - Integer reply: the list length after a successful insert operation.
// - Integer reply: 0 when the key doesn't exist.
// - Integer reply: -1 when the pivot wasn't found.
LInsert(ctx context.Context, key, op string, pivot, value any) IntCmd
LInsertBefore(ctx context.Context, key string, pivot, value any) IntCmd
LInsertAfter(ctx context.Context, key string, pivot, value any) IntCmd
// LMove
// Available since: 6.2.0
// Time complexity: O(1)
// ACL categories: @write @list @slow
// RESP2 / RESP3 Reply:
// - Bulk string reply: the element being popped and pushed.
LMove(ctx context.Context, source, destination, srcpos, destpos string) StringCmd
// LPop
// Available since: 1.0.0
// Time complexity: O(N) where N is the number of elements returned
// ACL categories: @write @list @fast
// RESP2 Reply:
// One of the following:
// - Nil reply: if the key does not exist.
// - Bulk string reply: when called without the count argument, the value of the first element.
// - Array reply: when called with the count argument, a list of popped elements.
// RESP3 Reply:
// One of the following:
// - Null reply: if the key does not exist.
// - Bulk string reply: when called without the count argument, the value of the first element.
// - Array reply: when called with the count argument, a list of popped elements.
// History:
// - Starting with Redis version 6.2.0: Added the count argument.
LPop(ctx context.Context, key string) StringCmd
LPopCount(ctx context.Context, key string, count int64) StringSliceCmd
// LMPop
// Available since: 7.0.0
// Time complexity: O(N+M) where N is the number of provided keys and M is the number of elements returned.
// ACL categories: @write, @list, @slow
// RESP2 Reply:
// One of the following:
// - Nil reply: if no element could be popped.
// - Array reply: a two-element array with the first element being the name of the key from which elements were popped and the second element being an array of elements.
// RESP3 Reply:
// One of the following:
// - Null reply: if no element could be popped.
// - Array reply: a two-element array with the first element being the name of the key from which elements were popped and the second element being an array of elements.
LMPop(ctx context.Context, direction string, count int64, keys ...string) KeyValuesCmd
// LPush
// Available since: 1.0.0
// Time complexity: O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments.
// ACL categories: @write @list @fast
// RESP2 / RESP3 Reply:
// - Integer reply: the length of the list after the push operation.
// History:
// - Starting with Redis version 2.4.0: Accepts multiple element arguments.
LPush(ctx context.Context, key string, values ...any) IntCmd
// LPushX
// Available since: 2.2.0
// Time complexity: O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments.
// ACL categories: @write @list @fast
// RESP2 / RESP3 Reply:
// - Integer reply: the length of the list after the push operation.
// History:
// - Starting with Redis version 4.0.0: Accepts multiple element arguments.
LPushX(ctx context.Context, key string, values ...any) IntCmd
// LRem
// Available since: 1.0.0
// Time complexity: O(N+M) where N is the length of the list and M is the number of elements removed.
// ACL categories: @write @list @slow
// RESP2 / RESP3 Reply:
// - Integer reply: the number of removed elements.
LRem(ctx context.Context, key string, count int64, value any) IntCmd
// LSet
// Available since: 1.0.0
// Time complexity: O(N) where N is the length of the list. Setting either the first or the last element of the list is O(1).
// ACL categories: @write @list @slow
// RESP2 / RESP3 Reply:
// - Simple string reply: OK.
LSet(ctx context.Context, key string, index int64, value any) StatusCmd
// LTrim
// Available since: 1.0.0
// Time complexity: O(N) where N is the number of elements to be removed by the operation.
// ACL categories: @write @list @slow
// RESP2 / RESP3 Reply:
// - Simple string reply: OK.
LTrim(ctx context.Context, key string, start, stop int64) StatusCmd
// RPop
// Available since: 1.0.0
// Time complexity: O(N) where N is the number of elements returned
// ACL categories: @write @list @fast
// RESP2 Reply:
// One of the following:
// - Nil reply: if the key does not exist.
// - Bulk string reply: when called without the count argument, the value of the last element.
// - Array reply: when called with the count argument, a list of popped elements.
// RESP3 Reply:
// One of the following:
// - Null reply: if the key does not exist.
// - Bulk string reply: when called without the count argument, the value of the last element.
// - Array reply: when called with the count argument, a list of popped elements.
// History:
// - Starting with Redis version 6.2.0: Added the count argument.
RPop(ctx context.Context, key string) StringCmd
RPopCount(ctx context.Context, key string, count int64) StringSliceCmd
// RPopLPush
// Available since: 1.2.0
// Time complexity: O(1)
// ACL categories: @write @list @slow
// RESP2 Reply:
// One of the following:
// - Bulk string reply: the element being popped and pushed.
// - Nil reply: if the source list is empty.
// RESP3 Reply:
// One of the following:
// - Bulk string reply: the element being popped and pushed.
// - Null reply: if the source list is empty.
RPopLPush(ctx context.Context, source, destination string) StringCmd
// RPush
// Available since: 1.0.0
// Time complexity: O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments.
// ACL categories: @write @list @fast
// RESP2 / RESP3 Reply:
// - Integer reply: the length of the list after the push operation.
// History:
// - Starting with Redis version 2.4.0: Accepts multiple element arguments.
RPush(ctx context.Context, key string, values ...any) IntCmd
// RPushX
// Available since: 2.2.0
// Time complexity: O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments.
// ACL categories: @write @list @fast
// RESP2 / RESP3 Reply:
// - Integer reply: the length of the list after the push operation.
// History:
// - Starting with Redis version 4.0.0: Accepts multiple element arguments.
RPushX(ctx context.Context, key string, values ...any) IntCmd
}
type ListReader interface {
// LPosCount
// vailable since: 6.0.6
// Time complexity: O(N) where N is the number of elements in the list, for the average case. When searching for elements near the head or the tail of the list,
// or when the MAXLEN option is provided, the command may run in constant time.
// ACL categories: @read @list @slow
// RESP2 Reply:
// Any of the following:
// - nil reply: if there is no matching element.
// - Integer reply: an integer representing the matching element.
// - Array reply: If the COUNT option is given, an array of integers representing the matching elements (or an empty array if there are no matches).
// RESP3 Reply:
// Any of the following:
// - Null reply: if there is no matching element.
// - Integer reply: an integer representing the matching element.
// - Array reply: If the COUNT option is given, an array of integers representing the matching elements (or an empty array if there are no matches).
LPosCount(ctx context.Context, key string, value string, count int64, args LPosArgs) IntSliceCmd
}
type ListCacheCmdable interface {
// LIndex
// Available since: 1.0.0
// Time complexity: O(N) where N is the number of elements to traverse to get to the element at index. This makes asking for the first or the last element of the list O(1).
// ACL categories: @read @list @slow
// RESP2 Reply:
// One of the following:
// - Nil reply: when index is out of range.
// - Bulk string reply: the requested element.
// RESP3 Reply:
// One of the following:
// - Null reply: when index is out of range.
// - Bulk string reply: the requested element.
LIndex(ctx context.Context, key string, index int64) StringCmd
// LLen
// Available since: 1.0.0
// Time complexity: O(1)
// ACL categories: @read @list @fast
// RESP2 / RESP3 Reply:
// - Integer reply: the length of the list.
LLen(ctx context.Context, key string) IntCmd
// LRange
// Available since: 1.0.0
// Time complexity: O(S+N) where S is the distance of start offset from HEAD for small lists, from nearest end (HEAD or TAIL) for large lists;
// and N is the number of elements in the specified range.
// ACL categories: @read @list @slow
// RESP2 / RESP3 Reply:
// - Array reply: a list of elements in the specified range, or an empty array if the key doesn't exist.
LRange(ctx context.Context, key string, start, stop int64) StringSliceCmd
// LPos
// vailable since: 6.0.6
// Time complexity: O(N) where N is the number of elements in the list, for the average case. When searching for elements near the head or the tail of the list,
// or when the MAXLEN option is provided, the command may run in constant time.
// ACL categories: @read @list @slow
// RESP2 Reply:
// One of the following:
// - nil reply: if there is no matching element.
// - Integer reply: an integer representing the matching element.
// - Array reply: If the COUNT option is given, an array of integers representing the matching elements (or an empty array if there are no matches).
// RESP3 Reply:
// One of the following:
// - Null reply: if there is no matching element.
// - Integer reply: an integer representing the matching element.
// - Array reply: If the COUNT option is given, an array of integers representing the matching elements (or an empty array if there are no matches).
LPos(ctx context.Context, key string, value string, args LPosArgs) IntCmd
}
func (c *client) BLMove(ctx context.Context, source, destination, srcpos, destpos string, timeout time.Duration) StringCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandBLMove, func() []string { return appendString(source, destination) })
r := newStringCmd(c.Do(ctx, c.builder.BLMoveCompleted(source, destination, srcpos, destpos, timeout)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) BLMPop(ctx context.Context, timeout time.Duration, direction string, count int64, keys ...string) KeyValuesCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandBLMPop, func() []string { return keys })
r := c.adapter.BLMPop(ctx, timeout, direction, count, keys...)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) BLPop(ctx context.Context, timeout time.Duration, keys ...string) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandBLPop, func() []string { return keys })
r := newStringSliceCmd(c.Do(ctx, c.builder.BLPopCompleted(timeout, keys...)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) BRPop(ctx context.Context, timeout time.Duration, keys ...string) StringSliceCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandBRPop, func() []string { return keys })
r := newStringSliceCmd(c.Do(ctx, c.builder.BRPopCompleted(timeout, keys...)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) BRPopLPush(ctx context.Context, source, destination string, timeout time.Duration) StringCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandBRPopLPush, func() []string { return appendString(source, destination) })
r := newStringCmd(c.Do(ctx, c.builder.BRPopLPushCompleted(source, destination, timeout)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LIndex(ctx context.Context, key string, index int64) StringCmd {
ctx = c.handler.before(ctx, CommandLIndex)
r := newStringCmd(c.Do(ctx, c.builder.LIndexCompleted(key, index)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LInsert(ctx context.Context, key, op string, pivot, value any) IntCmd {
switch strings.ToUpper(op) {
case BEFORE:
ctx = c.handler.before(ctx, CommandLInsertBefore)
case AFTER:
ctx = c.handler.before(ctx, CommandLInsertAfter)
default:
panic(fmt.Sprintf("Invalid op argument value: %s", op))
}
r := c.adapter.LInsert(ctx, key, op, pivot, value)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LInsertBefore(ctx context.Context, key string, pivot, value any) IntCmd {
ctx = c.handler.before(ctx, CommandLInsertBefore)
r := c.adapter.LInsertBefore(ctx, key, pivot, value)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LInsertAfter(ctx context.Context, key string, pivot, value any) IntCmd {
ctx = c.handler.before(ctx, CommandLInsertAfter)
r := c.adapter.LInsertAfter(ctx, key, pivot, value)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LLen(ctx context.Context, key string) IntCmd {
ctx = c.handler.before(ctx, CommandLLen)
var r IntCmd
if c.ttl > 0 {
r = newIntCmd(c.Do(ctx, c.builder.LLenCompleted(key)))
} else {
r = c.adapter.LLen(ctx, key)
}
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LMove(ctx context.Context, source, destination, srcpos, destpos string) StringCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandLMove, func() []string { return appendString(source, destination) })
r := newStringCmd(c.Do(ctx, c.builder.LMoveCompleted(source, destination, srcpos, destpos)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LPop(ctx context.Context, key string) StringCmd {
ctx = c.handler.before(ctx, CommandLPop)
r := newStringCmd(c.Do(ctx, c.builder.LPopCompleted(key)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LMPop(ctx context.Context, direction string, count int64, keys ...string) KeyValuesCmd {
ctx = c.handler.before(ctx, CommandLMPop)
r := c.adapter.LMPop(ctx, direction, count, keys...)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LPopCount(ctx context.Context, key string, count int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandLPopCount)
r := newStringSliceCmd(c.Do(ctx, c.builder.LPopCountCompleted(key, count)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LPos(ctx context.Context, key string, value string, args LPosArgs) IntCmd {
ctx = c.handler.before(ctx, CommandLPos)
var r IntCmd
if c.ttl > 0 {
r = newIntCmd(c.Do(ctx, c.builder.LPosCompleted(key, value, args)))
} else {
r = c.adapter.LPos(ctx, key, value, args)
}
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LPosCount(ctx context.Context, key string, value string, count int64, args LPosArgs) IntSliceCmd {
ctx = c.handler.before(ctx, CommandLPosCount)
r := c.adapter.LPosCount(ctx, key, value, count, args)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LPush(ctx context.Context, key string, values ...any) IntCmd {
if len(values) > 1 {
ctx = c.handler.before(ctx, CommandLMPush)
} else {
ctx = c.handler.before(ctx, CommandLPush)
}
r := c.adapter.LPush(ctx, key, values...)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LPushX(ctx context.Context, key string, values ...any) IntCmd {
if len(values) > 1 {
ctx = c.handler.before(ctx, CommandLMPushX)
} else {
ctx = c.handler.before(ctx, CommandLPushX)
}
r := c.adapter.LPushX(ctx, key, values...)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LRange(ctx context.Context, key string, start, stop int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandLRange)
r := newStringSliceCmd(c.Do(ctx, c.builder.LRangeCompleted(key, start, stop)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LRem(ctx context.Context, key string, count int64, value any) IntCmd {
ctx = c.handler.before(ctx, CommandLRem)
r := c.adapter.LRem(ctx, key, count, value)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LSet(ctx context.Context, key string, index int64, value any) StatusCmd {
ctx = c.handler.before(ctx, CommandLSet)
r := c.adapter.LSet(ctx, key, index, value)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) LTrim(ctx context.Context, key string, start, stop int64) StatusCmd {
ctx = c.handler.before(ctx, CommandLTrim)
r := c.adapter.LTrim(ctx, key, start, stop)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) RPop(ctx context.Context, key string) StringCmd {
ctx = c.handler.before(ctx, CommandRPop)
r := newStringCmd(c.Do(ctx, c.builder.RPopCompleted(key)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) RPopCount(ctx context.Context, key string, count int64) StringSliceCmd {
ctx = c.handler.before(ctx, CommandRPopCount)
r := newStringSliceCmd(c.Do(ctx, c.builder.RPopCountCompleted(key, count)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) RPopLPush(ctx context.Context, source, destination string) StringCmd {
ctx = c.handler.beforeWithKeys(ctx, CommandRPopLPush, func() []string { return appendString(source, destination) })
r := newStringCmd(c.Do(ctx, c.builder.RPopLPushCompleted(source, destination)))
c.handler.after(ctx, r.Err())
return r
}
func (c *client) RPush(ctx context.Context, key string, values ...any) IntCmd {
if len(values) > 1 {
ctx = c.handler.before(ctx, CommandRMPush)
} else {
ctx = c.handler.before(ctx, CommandRPush)
}
r := c.adapter.RPush(ctx, key, values...)
c.handler.after(ctx, r.Err())
return r
}
func (c *client) RPushX(ctx context.Context, key string, values ...any) IntCmd {
if len(values) > 1 {
ctx = c.handler.before(ctx, CommandRMPushX)
} else {
ctx = c.handler.before(ctx, CommandRPushX)
}
r := c.adapter.RPushX(ctx, key, values...)
c.handler.after(ctx, r.Err())
return r
}