forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.ts
More file actions
1340 lines (1252 loc) · 56.7 KB
/
Transaction.ts
File metadata and controls
1340 lines (1252 loc) · 56.7 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0
*/
import {
ExpireOptions,
InfoOptions,
RangeByIndex,
RangeByLex,
RangeByScore,
ScoreBoundary,
SetOptions,
StreamAddOptions,
StreamReadOptions,
StreamTrimOptions,
ZaddOptions,
createBlpop,
createBrpop,
createClientGetName,
createClientId,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
createConfigSet,
createCustomCommand,
createDecr,
createDecrBy,
createDel,
createEcho,
createExists,
createExpire,
createExpireAt,
createGet,
createHDel,
createHExists,
createHGet,
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHLen,
createHMGet,
createHSet,
createHSetNX,
createHvals,
createIncr,
createIncrBy,
createIncrByFloat,
createInfo,
createLLen,
createLPop,
createLPush,
createLRange,
createLRem,
createLTrim,
createLindex,
createMGet,
createMSet,
createPExpire,
createPExpireAt,
createPersist,
createPfAdd,
createPing,
createPttl,
createRPop,
createRPush,
createRename,
createSAdd,
createSCard,
createSMembers,
createSPop,
createSRem,
createSelect,
createSet,
createSismember,
createStrlen,
createTTL,
createTime,
createType,
createUnlink,
createXadd,
createXread,
createXtrim,
createZadd,
createZcard,
createZcount,
createZpopmax,
createZpopmin,
createZrange,
createZrangeWithScores,
createZrank,
createZrem,
createZremRangeByRank,
createZremRangeByScore,
createZscore,
} from "./Commands";
import { redis_request } from "./ProtobufMessage";
/**
* Base class encompassing shared commands for both standalone and cluster mode implementations in a transaction.
* Transactions allow the execution of a group of commands in a single step.
*
* Command Response:
* An array of command responses is returned by the client exec command, in the order they were given.
* Each element in the array represents a command given to the transaction.
* The response for each command depends on the executed Redis command.
* Specific response types are documented alongside each method.
*
* @example
* ```typescript
* const transaction = new BaseTransaction()
* .set("key", "value")
* .get("key");
* const result = await client.exec(transaction);
* console.log(result); // Output: ['OK', 'value']
* ```
*/
export class BaseTransaction<T extends BaseTransaction<T>> {
/**
* @internal
*/
readonly commands: redis_request.Command[] = [];
protected addAndReturn(command: redis_request.Command): T {
this.commands.push(command);
return this as unknown as T;
}
/** Get the value associated with the given key, or null if no such value exists.
* See https://redis.io/commands/get/ for details.
*
* @param key - The key to retrieve from the database.
*
* Command Response - If `key` exists, returns the value of `key` as a string. Otherwise, return null.
*/
public get(key: string): T {
return this.addAndReturn(createGet(key));
}
/** Set the given key with the given value. Return value is dependent on the passed options.
* See https://redis.io/commands/set/ for details.
*
* @param key - The key to store.
* @param value - The value to store with the given key.
* @param options - The set options.
*
* Command Response - If the value is successfully set, return OK.
* If `value` isn't set because of `onlyIfExists` or `onlyIfDoesNotExist` conditions, return null.
* If `returnOldValue` is set, return the old value as a string.
*/
public set(key: string, value: string, options?: SetOptions): T {
return this.addAndReturn(createSet(key, value, options));
}
/** Ping the Redis server.
* See https://redis.io/commands/ping/ for details.
*
* @param message - An optional message to include in the PING command.
* If not provided, the server will respond with "PONG".
* If provided, the server will respond with a copy of the message.
*
* Command Response - "PONG" if `message` is not provided, otherwise return a copy of `message`.
*/
public ping(message?: string): T {
return this.addAndReturn(createPing(message));
}
/** Get information and statistics about the Redis server.
* See https://redis.io/commands/info/ for details.
*
* @param options - A list of InfoSection values specifying which sections of information to retrieve.
* When no parameter is provided, the default option is assumed.
*
* Command Response - a string containing the information for the sections requested.
*/
public info(options?: InfoOptions[]): T {
return this.addAndReturn(createInfo(options));
}
/** Remove the specified keys. A key is ignored if it does not exist.
* See https://redis.io/commands/del/ for details.
*
* @param keys - A list of keys to be deleted from the database.
*
* Command Response - the number of keys that were removed.
*/
public del(keys: string[]): T {
return this.addAndReturn(createDel(keys));
}
/** Get the name of the connection on which the transaction is being executed.
* See https://redis.io/commands/client-getname/ for more details.
*
* Command Response - the name of the client connection as a string if a name is set, or null if no name is assigned.
*/
public clientGetName(): T {
return this.addAndReturn(createClientGetName());
}
/** Rewrite the configuration file with the current configuration.
* See https://redis.io/commands/select/ for details.
*
* Command Response - "OK" when the configuration was rewritten properly. Otherwise, the transaction fails with an error.
*/
public configRewrite(): T {
return this.addAndReturn(createConfigRewrite());
}
/** Resets the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands.
* See https://redis.io/commands/config-resetstat/ for details.
*
* Command Response - always "OK".
*/
public configResetStat(): T {
return this.addAndReturn(createConfigResetStat());
}
/** Retrieve the values of multiple keys.
* See https://redis.io/commands/mget/ for details.
*
* @param keys - A list of keys to retrieve values for.
*
* Command Response - A list of values corresponding to the provided keys. If a key is not found,
* its corresponding value in the list will be null.
*/
public mget(keys: string[]): T {
return this.addAndReturn(createMGet(keys));
}
/** Set multiple keys to multiple values in a single atomic operation.
* See https://redis.io/commands/mset/ for details.
*
* @param keyValueMap - A key-value map consisting of keys and their respective values to set.
*
* Command Response - always "OK".
*/
public mset(keyValueMap: Record<string, string>): T {
return this.addAndReturn(createMSet(keyValueMap));
}
/** Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/incr/ for details.
*
* @param key - The key to increment its value.
*
* Command Response - the value of `key` after the increment.
*/
public incr(key: string): T {
return this.addAndReturn(createIncr(key));
}
/** Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/incrby/ for details.
*
* @param key - The key to increment its value.
* @param amount - The amount to increment.
*
* Command Response - the value of `key` after the increment.
*/
public incrBy(key: string, amount: number): T {
return this.addAndReturn(createIncrBy(key, amount));
}
/** Increment the string representing a floating point number stored at `key` by `amount`.
* By using a negative amount value, the result is that the value stored at `key` is decremented.
* If `key` does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/incrbyfloat/ for details.
*
* @param key - The key to increment its value.
* @param amount - The amount to increment.
*
* Command Response - the value of `key` after the increment.
*
*/
public incrByFloat(key: string, amount: number): T {
return this.addAndReturn(createIncrByFloat(key, amount));
}
/** Returns the current connection id.
* See https://redis.io/commands/client-id/ for details.
*
* Command Response - the id of the client.
*/
public clientId(): T {
return this.addAndReturn(createClientId());
}
/** Decrements the number stored at `key` by one. If `key` does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/decr/ for details.
*
* @param key - The key to decrement its value.
*
* Command Response - the value of `key` after the decrement.
*/
public decr(key: string): T {
return this.addAndReturn(createDecr(key));
}
/** Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/decrby/ for details.
*
* @param key - The key to decrement its value.
* @param amount - The amount to decrement.
*
* Command Response - the value of `key` after the decrement.
*/
public decrBy(key: string, amount: number): T {
return this.addAndReturn(createDecrBy(key, amount));
}
/** Reads the configuration parameters of a running Redis server.
* See https://redis.io/commands/config-get/ for details.
*
* @param parameters - A list of configuration parameter names to retrieve values for.
*
* Command Response - A map of values corresponding to the configuration parameters.
*
*/
public configGet(parameters: string[]): T {
return this.addAndReturn(createConfigGet(parameters));
}
/** Set configuration parameters to the specified values.
* See https://redis.io/commands/config-set/ for details.
*
* @param parameters - A List of keyValuePairs consisting of configuration parameters and their respective values to set.
*
* Command Response - "OK" when the configuration was set properly. Otherwise, the transaction fails with an error.
*/
public configSet(parameters: Record<string, string>): T {
return this.addAndReturn(createConfigSet(parameters));
}
/** Retrieve the value associated with `field` in the hash stored at `key`.
* See https://redis.io/commands/hget/ for details.
*
* @param key - The key of the hash.
* @param field - The field in the hash stored at `key` to retrieve from the database.
*
* Command Response - the value associated with `field`, or null when `field` is not present in the hash or `key` does not exist.
*/
public hget(key: string, field: string): T {
return this.addAndReturn(createHGet(key, field));
}
/** Sets the specified fields to their respective values in the hash stored at `key`.
* See https://redis.io/commands/hset/ for details.
*
* @param key - The key of the hash.
* @param fieldValueMap - A field-value map consisting of fields and their corresponding values
* to be set in the hash stored at the specified key.
*
* Command Response - The number of fields that were added.
*/
public hset(key: string, fieldValueMap: Record<string, string>): T {
return this.addAndReturn(createHSet(key, fieldValueMap));
}
/** Sets `field` in the hash stored at `key` to `value`, only if `field` does not yet exist.
* If `key` does not exist, a new key holding a hash is created.
* If `field` already exists, this operation has no effect.
* See https://redis.io/commands/hsetnx/ for more details.
*
* @param key - The key of the hash.
* @param field - The field to set the value for.
* @param value - The value to set.
*
* Command Response - `true` if the field was set, `false` if the field already existed and was not set.
*/
public hsetnx(key: string, field: string, value: string): T {
return this.addAndReturn(createHSetNX(key, field, value));
}
/** Removes the specified fields from the hash stored at `key`.
* Specified fields that do not exist within this hash are ignored.
* See https://redis.io/commands/hdel/ for details.
*
* @param key - The key of the hash.
* @param fields - The fields to remove from the hash stored at `key`.
*
* Command Response - the number of fields that were removed from the hash, not including specified but non existing fields.
* If `key` does not exist, it is treated as an empty hash and it returns 0.
*/
public hdel(key: string, fields: string[]): T {
return this.addAndReturn(createHDel(key, fields));
}
/** Returns the values associated with the specified fields in the hash stored at `key`.
* See https://redis.io/commands/hmget/ for details.
*
* @param key - The key of the hash.
* @param fields - The fields in the hash stored at `key` to retrieve from the database.
*
* Command Response - a list of values associated with the given fields, in the same order as they are requested.
* For every field that does not exist in the hash, a null value is returned.
* If `key` does not exist, it is treated as an empty hash and it returns a list of null values.
*/
public hmget(key: string, fields: string[]): T {
return this.addAndReturn(createHMGet(key, fields));
}
/** Returns if `field` is an existing field in the hash stored at `key`.
* See https://redis.io/commands/hexists/ for details.
*
* @param key - The key of the hash.
* @param field - The field to check in the hash stored at `key`.
*
* Command Response - `true` if the hash contains `field`. If the hash does not contain `field`, or if `key` does not exist,
* the command response will be `false`.
*/
public hexists(key: string, field: string): T {
return this.addAndReturn(createHExists(key, field));
}
/** Returns all fields and values of the hash stored at `key`.
* See https://redis.io/commands/hgetall/ for details.
*
* @param key - The key of the hash.
*
* Command Response - a map of fields and their values stored in the hash. Every field name in the map is followed by its value.
* If `key` does not exist, it returns an empty map.
*/
public hgetall(key: string): T {
return this.addAndReturn(createHGetAll(key));
}
/** Increments the number stored at `field` in the hash stored at `key` by `increment`.
* By using a negative increment value, the value stored at `field` in the hash stored at `key` is decremented.
* If `field` or `key` does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/hincrby/ for details.
*
* @param key - The key of the hash.
* @param amount - The amount to increment.
* @param field - The field in the hash stored at `key` to increment its value.
*
* Command Response - the value of `field` in the hash stored at `key` after the increment.
*/
public hincrBy(key: string, field: string, amount: number): T {
return this.addAndReturn(createHIncrBy(key, field, amount));
}
/** Increment the string representing a floating point number stored at `field` in the hash stored at `key` by `increment`.
* By using a negative increment value, the value stored at `field` in the hash stored at `key` is decremented.
* If `field` or `key` does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/hincrbyfloat/ for details.
*
* @param key - The key of the hash.
* @param amount - The amount to increment.
* @param field - The field in the hash stored at `key` to increment its value.
*
* Command Response - the value of `field` in the hash stored at `key` after the increment.
*/
public hincrByFloat(key: string, field: string, amount: number): T {
return this.addAndReturn(createHIncrByFloat(key, field, amount));
}
/** Returns the number of fields contained in the hash stored at `key`.
* See https://redis.io/commands/hlen/ for more details.
*
* @param key - The key of the hash.
*
* Command Response - The number of fields in the hash, or 0 when the key does not exist.
*/
public hlen(key: string): T {
return this.addAndReturn(createHLen(key));
}
/** Returns all values in the hash stored at key.
* See https://redis.io/commands/hvals/ for more details.
*
* @param key - The key of the hash.
*
* Command Response - a list of values in the hash, or an empty list when the key does not exist.
*/
public hvals(key: string): T {
return this.addAndReturn(createHvals(key));
}
/** Inserts all the specified values at the head of the list stored at `key`.
* `elements` are inserted one after the other to the head of the list, from the leftmost element to the rightmost element.
* If `key` does not exist, it is created as empty list before performing the push operations.
* See https://redis.io/commands/lpush/ for details.
*
* @param key - The key of the list.
* @param elements - The elements to insert at the head of the list stored at `key`.
*
* Command Response - the length of the list after the push operations.
*/
public lpush(key: string, elements: string[]): T {
return this.addAndReturn(createLPush(key, elements));
}
/** Removes and returns the first elements of the list stored at `key`.
* The command pops a single element from the beginning of the list.
* See https://redis.io/commands/lpop/ for details.
*
* @param key - The key of the list.
*
* Command Response - The value of the first element.
* If `key` does not exist null will be returned.
*/
public lpop(key: string): T {
return this.addAndReturn(createLPop(key));
}
/** Removes and returns up to `count` elements of the list stored at `key`, depending on the list's length.
* See https://redis.io/commands/lpop/ for details.
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
*
* Command Response - A list of the popped elements will be returned depending on the list's length.
* If `key` does not exist null will be returned.
*/
public lpopCount(key: string, count: number): T {
return this.addAndReturn(createLPop(key, count));
}
/** Returns the specified elements of the list stored at `key`.
* The offsets `start` and `end` are zero-based indexes, with 0 being the first element of the list, 1 being the next element and so on.
* These offsets can also be negative numbers indicating offsets starting at the end of the list,
* with -1 being the last element of the list, -2 being the penultimate, and so on.
* See https://redis.io/commands/lrange/ for details.
*
* @param key - The key of the list.
* @param start - The starting point of the range.
* @param end - The end of the range.
*
* Command Response - list of elements in the specified range.
* If `start` exceeds the end of the list, or if `start` is greater than `end`, an empty list will be returned.
* If `end` exceeds the actual end of the list, the range will stop at the actual end of the list.
* If `key` does not exist an empty list will be returned.
*/
public lrange(key: string, start: number, end: number): T {
return this.addAndReturn(createLRange(key, start, end));
}
/** Returns the length of the list stored at `key`.
* See https://redis.io/commands/llen/ for details.
*
* @param key - The key of the list.
*
* Command Response - the length of the list at `key`.
* If `key` does not exist, it is interpreted as an empty list and 0 is returned.
*/
public llen(key: string): T {
return this.addAndReturn(createLLen(key));
}
/** Trim an existing list so that it will contain only the specified range of elements specified.
* The offsets `start` and `end` are zero-based indexes, with 0 being the first element of the list, 1 being the next element and so on.
* These offsets can also be negative numbers indicating offsets starting at the end of the list,
* with -1 being the last element of the list, -2 being the penultimate, and so on.
* See https://redis.io/commands/ltrim/ for details.
*
* @param key - The key of the list.
* @param start - The starting point of the range.
* @param end - The end of the range.
*
* Command Response - always "OK".
* If `start` exceeds the end of the list, or if `start` is greater than `end`, the result will be an empty list (which causes key to be removed).
* If `end` exceeds the actual end of the list, it will be treated like the last element of the list.
* If `key` does not exist the command will be ignored.
*/
public ltrim(key: string, start: number, end: number): T {
return this.addAndReturn(createLTrim(key, start, end));
}
/** Removes the first `count` occurrences of elements equal to `element` from the list stored at `key`.
* If `count` is positive : Removes elements equal to `element` moving from head to tail.
* If `count` is negative : Removes elements equal to `element` moving from tail to head.
* If `count` is 0 or `count` is greater than the occurrences of elements equal to `element`: Removes all elements equal to `element`.
*
* @param key - The key of the list.
* @param count - The count of the occurrences of elements equal to `element` to remove.
* @param element - The element to remove from the list.
*
* Command Response - the number of the removed elements.
* If `key` does not exist, 0 is returned.
*/
public lrem(key: string, count: number, element: string): T {
return this.addAndReturn(createLRem(key, count, element));
}
/** Inserts all the specified values at the tail of the list stored at `key`.
* `elements` are inserted one after the other to the tail of the list, from the leftmost element to the rightmost element.
* If `key` does not exist, it is created as empty list before performing the push operations.
* See https://redis.io/commands/rpush/ for details.
*
* @param key - The key of the list.
* @param elements - The elements to insert at the tail of the list stored at `key`.
*
* Command Response - the length of the list after the push operations.
*/
public rpush(key: string, elements: string[]): T {
return this.addAndReturn(createRPush(key, elements));
}
/** Removes and returns the last elements of the list stored at `key`.
* The command pops a single element from the end of the list.
* See https://redis.io/commands/rpop/ for details.
*
* @param key - The key of the list.
*
* Command Response - The value of the last element.
* If `key` does not exist null will be returned.
*/
public rpop(key: string): T {
return this.addAndReturn(createRPop(key));
}
/** Removes and returns up to `count` elements from the list stored at `key`, depending on the list's length.
* See https://redis.io/commands/rpop/ for details.
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
*
* Command Response - A list of popped elements will be returned depending on the list's length.
* If `key` does not exist null will be returned.
*/
public rpopCount(key: string, count: number): T {
return this.addAndReturn(createRPop(key, count));
}
/** Adds the specified members to the set stored at `key`. Specified members that are already a member of this set are ignored.
* If `key` does not exist, a new set is created before adding `members`.
* See https://redis.io/commands/sadd/ for details.
*
* @param key - The key to store the members to its set.
* @param members - A list of members to add to the set stored at `key`.
*
* Command Response - the number of members that were added to the set, not including all the members already present in the set.
*/
public sadd(key: string, members: string[]): T {
return this.addAndReturn(createSAdd(key, members));
}
/** Removes the specified members from the set stored at `key`. Specified members that are not a member of this set are ignored.
* See https://redis.io/commands/srem/ for details.
*
* @param key - The key to remove the members from its set.
* @param members - A list of members to remove from the set stored at `key`.
*
* Command Response - the number of members that were removed from the set, not including non existing members.
* If `key` does not exist, it is treated as an empty set and this command returns 0.
*/
public srem(key: string, members: string[]): T {
return this.addAndReturn(createSRem(key, members));
}
/** Returns all the members of the set value stored at `key`.
* See https://redis.io/commands/smembers/ for details.
*
* @param key - The key to return its members.
*
* Command Response - all members of the set.
* If `key` does not exist, it is treated as an empty set and this command returns empty list.
*/
public smembers(key: string): T {
return this.addAndReturn(createSMembers(key));
}
/** Returns the set cardinality (number of elements) of the set stored at `key`.
* See https://redis.io/commands/scard/ for details.
*
* @param key - The key to return the number of its members.
*
* Command Response - the cardinality (number of elements) of the set, or 0 if key does not exist.
*/
public scard(key: string): T {
return this.addAndReturn(createSCard(key));
}
/** Returns if `member` is a member of the set stored at `key`.
* See https://redis.io/commands/sismember/ for more details.
*
* @param key - The key of the set.
* @param member - The member to check for existence in the set.
*
* Command Response - `true` if the member exists in the set, `false` otherwise.
* If `key` doesn't exist, it is treated as an empty set and the command returns `false`.
*/
public sismember(key: string, member: string): T {
return this.addAndReturn(createSismember(key, member));
}
/** Removes and returns one random member from the set value store at `key`.
* See https://redis.io/commands/spop/ for details.
* To pop multiple members, see `spopCount`.
*
* @param key - The key of the set.
*
* Command Response - the value of the popped member.
* If `key` does not exist, null will be returned.
*/
public spop(key: string): T {
return this.addAndReturn(createSPop(key));
}
/** Removes and returns up to `count` random members from the set value store at `key`, depending on the set's length.
* See https://redis.io/commands/spop/ for details.
*
* @param key - The key of the set.
* @param count - The count of the elements to pop from the set.
*
* Command Response - A list of popped elements will be returned depending on the set's length.
* If `key` does not exist, empty list will be returned.
*/
public spopCount(key: string, count: number): T {
return this.addAndReturn(createSPop(key, count));
}
/** Returns the number of keys in `keys` that exist in the database.
* See https://redis.io/commands/exists/ for details.
*
* @param keys - The keys list to check.
*
* Command Response - the number of keys that exist. If the same existing key is mentioned in `keys` multiple times,
* it will be counted multiple times.
*/
public exists(keys: string[]): T {
return this.addAndReturn(createExists(keys));
}
/** Removes the specified keys. A key is ignored if it does not exist.
* This command, similar to DEL, removes specified keys and ignores non-existent ones.
* However, this command does not block the server, while [DEL](https://redis.io/commands/del) does.
* See https://redis.io/commands/unlink/ for details.
*
* @param keys - The keys we wanted to unlink.
*
* Command Response - the number of keys that were unlinked.
*/
public unlink(keys: string[]): T {
return this.addAndReturn(createUnlink(keys));
}
/** Sets a timeout on `key` in seconds. After the timeout has expired, the key will automatically be deleted.
* If `key` already has an existing expire set, the time to live is updated to the new value.
* If `seconds` is non-positive number, the key will be deleted rather than expired.
* The timeout will only be cleared by commands that delete or overwrite the contents of `key`.
* See https://redis.io/commands/expire/ for details.
*
* @param key - The key to set timeout on it.
* @param seconds - The timeout in seconds.
* @param option - The expire option.
*
* Command Response - `true` if the timeout was set. `false` if the timeout was not set. e.g. key doesn't exist,
* or operation skipped due to the provided arguments.
*/
public expire(key: string, seconds: number, option?: ExpireOptions): T {
return this.addAndReturn(createExpire(key, seconds, option));
}
/** Sets a timeout on `key`. It takes an absolute Unix timestamp (seconds since January 1, 1970) instead of specifying the number of seconds.
* A timestamp in the past will delete the key immediately. After the timeout has expired, the key will automatically be deleted.
* If `key` already has an existing expire set, the time to live is updated to the new value.
* The timeout will only be cleared by commands that delete or overwrite the contents of `key`.
* See https://redis.io/commands/expireat/ for details.
*
* @param key - The key to set timeout on it.
* @param unixSeconds - The timeout in an absolute Unix timestamp.
* @param option - The expire option.
*
* Command Response - `true` if the timeout was set. `false` if the timeout was not set. e.g. key doesn't exist,
* or operation skipped due to the provided arguments.
*/
public expireAt(
key: string,
unixSeconds: number,
option?: ExpireOptions,
): T {
return this.addAndReturn(createExpireAt(key, unixSeconds, option));
}
/** Sets a timeout on `key` in milliseconds. After the timeout has expired, the key will automatically be deleted.
* If `key` already has an existing expire set, the time to live is updated to the new value.
* If `milliseconds` is non-positive number, the key will be deleted rather than expired.
* The timeout will only be cleared by commands that delete or overwrite the contents of `key`.
* See https://redis.io/commands/pexpire/ for details.
*
* @param key - The key to set timeout on it.
* @param milliseconds - The timeout in milliseconds.
* @param option - The expire option.
*
* Command Response - `true` if the timeout was set. `false` if the timeout was not set. e.g. key doesn't exist,
* or operation skipped due to the provided arguments.
*/
public pexpire(
key: string,
milliseconds: number,
option?: ExpireOptions,
): T {
return this.addAndReturn(createPExpire(key, milliseconds, option));
}
/** Sets a timeout on `key`. It takes an absolute Unix timestamp (milliseconds since January 1, 1970) instead of specifying the number of milliseconds.
* A timestamp in the past will delete the key immediately. After the timeout has expired, the key will automatically be deleted.
* If `key` already has an existing expire set, the time to live is updated to the new value.
* The timeout will only be cleared by commands that delete or overwrite the contents of `key`.
* See https://redis.io/commands/pexpireat/ for details.
*
* @param key - The key to set timeout on it.
* @param unixMilliseconds - The timeout in an absolute Unix timestamp.
* @param option - The expire option.
*
* Command Response - `true` if the timeout was set. `false` if the timeout was not set. e.g. key doesn't exist,
* or operation skipped due to the provided arguments.
*/
public pexpireAt(
key: string,
unixMilliseconds: number,
option?: ExpireOptions,
): T {
return this.addAndReturn(
createPExpireAt(key, unixMilliseconds, option),
);
}
/** Returns the remaining time to live of `key` that has a timeout.
* See https://redis.io/commands/ttl/ for details.
*
* @param key - The key to return its timeout.
*
* Command Response - TTL in seconds, -2 if `key` does not exist or -1 if `key` exists but has no associated expire.
*/
public ttl(key: string): T {
return this.addAndReturn(createTTL(key));
}
/** Adds members with their scores to the sorted set stored at `key`.
* If a member is already a part of the sorted set, its score is updated.
* See https://redis.io/commands/zadd/ for more details.
*
* @param key - The key of the sorted set.
* @param membersScoresMap - A mapping of members to their corresponding scores.
* @param options - The Zadd options.
* @param changed - Modify the return value from the number of new elements added, to the total number of elements changed.
*
* Command Response - The number of elements added to the sorted set.
* If `changed` is set, returns the number of elements updated in the sorted set.
*/
public zadd(
key: string,
membersScoresMap: Record<string, number>,
options?: ZaddOptions,
changed?: boolean,
): T {
return this.addAndReturn(
createZadd(
key,
membersScoresMap,
options,
changed ? "CH" : undefined,
),
);
}
/** Increments the score of member in the sorted set stored at `key` by `increment`.
* If `member` does not exist in the sorted set, it is added with `increment` as its score (as if its previous score was 0.0).
* If `key` does not exist, a new sorted set with the specified member as its sole member is created.
* See https://redis.io/commands/zadd/ for more details.
*
* @param key - The key of the sorted set.
* @param member - A member in the sorted set to increment.
* @param increment - The score to increment the member.
* @param options - The Zadd options.
*
* Command Response - The score of the member.
* If there was a conflict with the options, the operation aborts and null is returned.
*/
public zaddIncr(
key: string,
member: string,
increment: number,
options?: ZaddOptions,
): T {
return this.addAndReturn(
createZadd(key, { [member]: increment }, options, "INCR"),
);
}
/** Removes the specified members from the sorted set stored at `key`.
* Specified members that are not a member of this set are ignored.
* See https://redis.io/commands/zrem/ for more details.
*
* @param key - The key of the sorted set.
* @param members - A list of members to remove from the sorted set.
*
* Command Response - The number of members that were removed from the sorted set, not including non-existing members.
* If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
*/
public zrem(key: string, members: string[]): T {
return this.addAndReturn(createZrem(key, members));
}
/** Returns the cardinality (number of elements) of the sorted set stored at `key`.
* See https://redis.io/commands/zcard/ for more details.
*
* @param key - The key of the sorted set.
*
* Command Response - The number of elements in the sorted set.
* If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
*/
public zcard(key: string): T {
return this.addAndReturn(createZcard(key));
}
/** Returns the score of `member` in the sorted set stored at `key`.
* See https://redis.io/commands/zscore/ for more details.
*
* @param key - The key of the sorted set.
* @param member - The member whose score is to be retrieved.
*
* Command Response - The score of the member.
* If `member` does not exist in the sorted set, null is returned.
* If `key` does not exist, null is returned.
*/
public zscore(key: string, member: string): T {
return this.addAndReturn(createZscore(key, member));
}
/** Returns the number of members in the sorted set stored at `key` with scores between `minScore` and `maxScore`.
* See https://redis.io/commands/zcount/ for more details.
*
* @param key - The key of the sorted set.
* @param minScore - The minimum score to count from. Can be positive/negative infinity, or specific score and inclusivity.
* @param maxScore - The maximum score to count up to. Can be positive/negative infinity, or specific score and inclusivity.
*
* Command Response - The number of members in the specified score range.
* If `key` does not exist, it is treated as an empty sorted set, and the command returns 0.
* If `minScore` is greater than `maxScore`, 0 is returned.
*/
public zcount(
key: string,
minScore: ScoreBoundary<number>,
maxScore: ScoreBoundary<number>,
): T {
return this.addAndReturn(createZcount(key, minScore, maxScore));
}
/** Returns the specified range of elements in the sorted set stored at `key`.
* ZRANGE can perform different types of range queries: by index (rank), by the score, or by lexicographical order.
*
* See https://redis.io/commands/zrange/ for more details.
* To get the elements with their scores, see `zrangeWithScores`.
*
* @param key - The key of the sorted set.
* @param rangeQuery - The range query object representing the type of range query to perform.
* For range queries by index (rank), use RangeByIndex.
* For range queries by lexicographical order, use RangeByLex.
* For range queries by score, use RangeByScore.
* @param reverse - If true, reverses the sorted set, with index 0 as the element with the highest score.
*
* Command Response - A list of elements within the specified range.
* If `key` does not exist, it is treated as an empty sorted set, and the command returns an empty array.
*/
public zrange(
key: string,
rangeQuery: RangeByScore | RangeByLex | RangeByIndex,
reverse: boolean = false,
): T {
return this.addAndReturn(createZrange(key, rangeQuery, reverse));
}
/** Returns the specified range of elements with their scores in the sorted set stored at `key`.
* Similar to ZRANGE but with a WITHSCORE flag.
* See https://redis.io/commands/zrange/ for more details.
*
* @param key - The key of the sorted set.
* @param rangeQuery - The range query object representing the type of range query to perform.
* For range queries by index (rank), use RangeByIndex.
* For range queries by lexicographical order, use RangeByLex.
* For range queries by score, use RangeByScore.
* @param reverse - If true, reverses the sorted set, with index 0 as the element with the highest score.
*
* Command Response - A map of elements and their scores within the specified range.
* If `key` does not exist, it is treated as an empty sorted set, and the command returns an empty map.
*/
public zrangeWithScores(
key: string,
rangeQuery: RangeByScore | RangeByLex | RangeByIndex,
reverse: boolean = false,
): T {
return this.addAndReturn(
createZrangeWithScores(key, rangeQuery, reverse),
);
}
/** Returns the string representation of the type of the value stored at `key`.
* See https://redis.io/commands/type/ for more details.
*
* @param key - The key to check its data type.
*
* Command Response - If the key exists, the type of the stored value is returned. Otherwise, a "none" string is returned.
*/
public type(key: string): T {
return this.addAndReturn(createType(key));
}
/** Returns the length of the string value stored at `key`.