0
# Commands and Operations
1
2
This document covers Redis command interfaces organized by data types and operations, providing comprehensive coverage of all Redis functionality.
3
4
## Core Command Interfaces
5
6
### JedisCommands
7
8
Primary interface defining all Redis string-based commands.
9
10
```java { .api }
11
public interface JedisCommands extends KeyCommands, StringCommands, ListCommands, HashCommands,
12
SetCommands, SortedSetCommands, GeoCommands, HyperLogLogCommands, StreamCommands,
13
ScriptingKeyCommands, FunctionCommands {
14
// Inherits all Redis commands from specialized interfaces
15
}
16
```
17
18
### JedisBinaryCommands
19
20
Interface for Redis commands that work with binary data.
21
22
```java { .api }
23
public interface JedisBinaryCommands extends KeyBinaryCommands, StringBinaryCommands,
24
ListBinaryCommands, HashBinaryCommands, SetBinaryCommands, SortedSetBinaryCommands,
25
GeoBinaryCommands, HyperLogLogBinaryCommands, StreamBinaryCommands, ScriptingKeyBinaryCommands,
26
FunctionBinaryCommands {
27
// Inherits all binary Redis commands
28
}
29
```
30
31
## String Operations
32
33
### StringCommands
34
35
Commands for Redis string data type operations.
36
37
```java { .api }
38
public interface StringCommands {
39
/**
40
* Set key to hold string value
41
* @param key Redis key
42
* @param value String value
43
* @return Status code reply
44
*/
45
String set(String key, String value);
46
47
/**
48
* Set key with additional parameters
49
* @param key Redis key
50
* @param value String value
51
* @param params SET command parameters (EX, PX, NX, XX, etc.)
52
* @return Status code reply or null if conditions not met
53
*/
54
String set(String key, String value, SetParams params);
55
56
/**
57
* Get value of key
58
* @param key Redis key
59
* @return Value at key or null if key doesn't exist
60
*/
61
String get(String key);
62
63
/**
64
* Get value of key and set new value atomically
65
* @param key Redis key
66
* @param value New value
67
* @return Previous value at key
68
*/
69
String getSet(String key, String value);
70
71
/**
72
* Get value and optionally set expiration
73
* @param key Redis key
74
* @param params GETEX parameters for expiration
75
* @return Value at key
76
*/
77
String getEx(String key, GetExParams params);
78
79
/**
80
* Get substring of value at key
81
* @param key Redis key
82
* @param startOffset Start position (inclusive)
83
* @param endOffset End position (inclusive)
84
* @return Substring of value
85
*/
86
String getrange(String key, long startOffset, long endOffset);
87
88
/**
89
* Set value at key and return old value
90
* @param key Redis key
91
* @param value New value
92
* @return Old value at key
93
*/
94
String getDel(String key);
95
96
/**
97
* Set multiple keys to multiple values
98
* @param keysvalues Alternating keys and values
99
* @return Status code reply
100
*/
101
String mset(String... keysvalues);
102
103
/**
104
* Set multiple keys only if none exist
105
* @param keysvalues Alternating keys and values
106
* @return 1 if all keys set, 0 if any key exists
107
*/
108
Long msetnx(String... keysvalues);
109
110
/**
111
* Get values of multiple keys
112
* @param keys Redis keys
113
* @return List of values (null for non-existent keys)
114
*/
115
List<String> mget(String... keys);
116
117
/**
118
* Increment value at key
119
* @param key Redis key containing integer value
120
* @return Value after increment
121
*/
122
Long incr(String key);
123
124
/**
125
* Increment value at key by increment
126
* @param key Redis key containing integer value
127
* @param increment Amount to increment
128
* @return Value after increment
129
*/
130
Long incrBy(String key, long increment);
131
132
/**
133
* Increment float value at key
134
* @param key Redis key containing float value
135
* @param increment Float increment amount
136
* @return Value after increment
137
*/
138
Double incrByFloat(String key, double increment);
139
140
/**
141
* Decrement value at key
142
* @param key Redis key containing integer value
143
* @return Value after decrement
144
*/
145
Long decr(String key);
146
147
/**
148
* Decrement value at key by decrement
149
* @param key Redis key containing integer value
150
* @param decrement Amount to decrement
151
* @return Value after decrement
152
*/
153
Long decrBy(String key, long decrement);
154
155
/**
156
* Append value to key
157
* @param key Redis key
158
* @param value Value to append
159
* @return Length of string after append
160
*/
161
Long append(String key, String value);
162
163
/**
164
* Get length of string at key
165
* @param key Redis key
166
* @return Length of string or 0 if key doesn't exist
167
*/
168
Long strlen(String key);
169
}
170
```
171
172
#### Usage Example
173
174
```java
175
jedis.set("counter", "0");
176
jedis.incr("counter"); // Returns 1
177
jedis.incrBy("counter", 5); // Returns 6
178
179
jedis.set("user:1:name", "John");
180
jedis.append("user:1:name", " Doe"); // "John Doe"
181
182
// Multiple operations
183
jedis.mset("key1", "value1", "key2", "value2", "key3", "value3");
184
List<String> values = jedis.mget("key1", "key2", "key3");
185
186
// Conditional set
187
SetParams params = SetParams.setParams().nx().ex(3600);
188
String result = jedis.set("session:abc", "userdata", params);
189
```
190
191
## Hash Operations
192
193
### HashCommands
194
195
Commands for Redis hash data type operations.
196
197
```java { .api }
198
public interface HashCommands {
199
/**
200
* Set field in hash to value
201
* @param key Hash key
202
* @param field Hash field
203
* @param value Field value
204
* @return 1 if new field, 0 if field updated
205
*/
206
Long hset(String key, String field, String value);
207
208
/**
209
* Set multiple fields in hash
210
* @param key Hash key
211
* @param hash Map of field-value pairs
212
* @return Number of fields added (not including updated fields)
213
*/
214
Long hset(String key, Map<String, String> hash);
215
216
/**
217
* Get value of hash field
218
* @param key Hash key
219
* @param field Hash field
220
* @return Field value or null if field doesn't exist
221
*/
222
String hget(String key, String field);
223
224
/**
225
* Get all fields and values in hash
226
* @param key Hash key
227
* @return Map of all field-value pairs
228
*/
229
Map<String, String> hgetAll(String key);
230
231
/**
232
* Get values of multiple hash fields
233
* @param key Hash key
234
* @param fields Hash fields
235
* @return List of field values (null for non-existent fields)
236
*/
237
List<String> hmget(String key, String... fields);
238
239
/**
240
* Set field only if it doesn't exist
241
* @param key Hash key
242
* @param field Hash field
243
* @param value Field value
244
* @return 1 if field set, 0 if field exists
245
*/
246
Long hsetnx(String key, String field, String value);
247
248
/**
249
* Delete hash fields
250
* @param key Hash key
251
* @param fields Fields to delete
252
* @return Number of fields deleted
253
*/
254
Long hdel(String key, String... fields);
255
256
/**
257
* Check if hash field exists
258
* @param key Hash key
259
* @param field Hash field
260
* @return true if field exists
261
*/
262
Boolean hexists(String key, String field);
263
264
/**
265
* Get number of fields in hash
266
* @param key Hash key
267
* @return Number of fields
268
*/
269
Long hlen(String key);
270
271
/**
272
* Get all field names in hash
273
* @param key Hash key
274
* @return Set of field names
275
*/
276
Set<String> hkeys(String key);
277
278
/**
279
* Get all values in hash
280
* @param key Hash key
281
* @return List of field values
282
*/
283
List<String> hvals(String key);
284
285
/**
286
* Increment hash field by increment
287
* @param key Hash key
288
* @param field Hash field
289
* @param value Increment amount
290
* @return Value after increment
291
*/
292
Long hincrBy(String key, String field, long value);
293
294
/**
295
* Increment hash field by float increment
296
* @param key Hash key
297
* @param field Hash field
298
* @param value Float increment amount
299
* @return Value after increment
300
*/
301
Double hincrByFloat(String key, String field, double value);
302
303
/**
304
* Scan hash fields and values
305
* @param key Hash key
306
* @param cursor Scan cursor
307
* @return Scan result with cursor and field-value pairs
308
*/
309
ScanResult<Map.Entry<String, String>> hscan(String key, String cursor);
310
311
/**
312
* Scan hash with parameters
313
* @param key Hash key
314
* @param cursor Scan cursor
315
* @param params Scan parameters (MATCH, COUNT)
316
* @return Scan result with cursor and field-value pairs
317
*/
318
ScanResult<Map.Entry<String, String>> hscan(String key, String cursor, ScanParams params);
319
320
/**
321
* Get length of hash field value
322
* @param key Hash key
323
* @param field Hash field
324
* @return Length of field value or 0 if field doesn't exist
325
*/
326
Long hstrlen(String key, String field);
327
}
328
```
329
330
#### Usage Example
331
332
```java
333
// User profile as hash
334
jedis.hset("user:1", "name", "John Doe");
335
jedis.hset("user:1", "email", "john@example.com");
336
jedis.hset("user:1", "age", "30");
337
338
// Get specific field
339
String name = jedis.hget("user:1", "name");
340
341
// Get all user data
342
Map<String, String> user = jedis.hgetAll("user:1");
343
344
// Increment age
345
jedis.hincrBy("user:1", "age", 1);
346
347
// Bulk operations
348
Map<String, String> profile = new HashMap<>();
349
profile.put("country", "USA");
350
profile.put("city", "New York");
351
jedis.hset("user:1", profile);
352
353
// Check if field exists
354
boolean hasPhone = jedis.hexists("user:1", "phone");
355
```
356
357
## List Operations
358
359
### ListCommands
360
361
Commands for Redis list data type operations.
362
363
```java { .api }
364
public interface ListCommands {
365
/**
366
* Push elements to head of list
367
* @param key List key
368
* @param strings Elements to push
369
* @return Length of list after push
370
*/
371
Long lpush(String key, String... strings);
372
373
/**
374
* Push elements to tail of list
375
* @param key List key
376
* @param strings Elements to push
377
* @return Length of list after push
378
*/
379
Long rpush(String key, String... strings);
380
381
/**
382
* Push element to head only if list exists
383
* @param key List key
384
* @param strings Elements to push
385
* @return Length of list after push or 0 if list doesn't exist
386
*/
387
Long lpushx(String key, String... strings);
388
389
/**
390
* Push element to tail only if list exists
391
* @param key List key
392
* @param strings Elements to push
393
* @return Length of list after push or 0 if list doesn't exist
394
*/
395
Long rpushx(String key, String... strings);
396
397
/**
398
* Pop element from head of list
399
* @param key List key
400
* @return Head element or null if list is empty
401
*/
402
String lpop(String key);
403
404
/**
405
* Pop multiple elements from head of list
406
* @param key List key
407
* @param count Number of elements to pop
408
* @return List of popped elements
409
*/
410
List<String> lpop(String key, int count);
411
412
/**
413
* Pop element from tail of list
414
* @param key List key
415
* @return Tail element or null if list is empty
416
*/
417
String rpop(String key);
418
419
/**
420
* Pop multiple elements from tail of list
421
* @param key List key
422
* @param count Number of elements to pop
423
* @return List of popped elements
424
*/
425
List<String> rpop(String key, int count);
426
427
/**
428
* Pop from tail of source list and push to head of destination
429
* @param srckey Source list key
430
* @param dstkey Destination list key
431
* @return Element moved or null if source list is empty
432
*/
433
String rpoplpush(String srckey, String dstkey);
434
435
/**
436
* Get list element at index
437
* @param key List key
438
* @param index Element index (0-based, negative for tail-relative)
439
* @return Element at index or null if index out of range
440
*/
441
String lindex(String key, long index);
442
443
/**
444
* Get list length
445
* @param key List key
446
* @return List length
447
*/
448
Long llen(String key);
449
450
/**
451
* Get range of elements from list
452
* @param key List key
453
* @param start Start index (inclusive)
454
* @param stop Stop index (inclusive, -1 for end)
455
* @return List of elements in range
456
*/
457
List<String> lrange(String key, long start, long stop);
458
459
/**
460
* Set list element at index
461
* @param key List key
462
* @param index Element index
463
* @param value New element value
464
* @return Status code reply
465
*/
466
String lset(String key, long index, String value);
467
468
/**
469
* Trim list to specified range
470
* @param key List key
471
* @param start Start index (inclusive)
472
* @param stop Stop index (inclusive)
473
* @return Status code reply
474
*/
475
String ltrim(String key, long start, long stop);
476
477
/**
478
* Remove elements from list
479
* @param key List key
480
* @param count Number to remove (0=all, >0=from head, <0=from tail)
481
* @param element Element value to remove
482
* @return Number of elements removed
483
*/
484
Long lrem(String key, long count, String element);
485
486
/**
487
* Insert element before or after pivot
488
* @param key List key
489
* @param where INSERT position (BEFORE or AFTER)
490
* @param pivot Pivot element
491
* @param element Element to insert
492
* @return List length after insert or -1 if pivot not found
493
*/
494
Long linsert(String key, ListPosition where, String pivot, String element);
495
496
/**
497
* Find position of element in list
498
* @param key List key
499
* @param element Element to find
500
* @return Index of first occurrence or null if not found
501
*/
502
Long lpos(String key, String element);
503
504
/**
505
* Find positions of element in list with parameters
506
* @param key List key
507
* @param element Element to find
508
* @param params LPOS parameters (RANK, COUNT, MAXLEN)
509
* @return List of indices where element occurs
510
*/
511
List<Long> lpos(String key, String element, LPosParams params);
512
513
/**
514
* Block until element available and pop from head
515
* @param timeout Timeout in seconds (0 for infinite)
516
* @param keys List keys to check
517
* @return List with key name and popped element
518
*/
519
List<String> blpop(int timeout, String... keys);
520
521
/**
522
* Block until element available and pop from tail
523
* @param timeout Timeout in seconds (0 for infinite)
524
* @param keys List keys to check
525
* @return List with key name and popped element
526
*/
527
List<String> brpop(int timeout, String... keys);
528
529
/**
530
* Block, pop from tail of source and push to head of destination
531
* @param source Source list key
532
* @param destination Destination list key
533
* @param timeout Timeout in seconds
534
* @return Element moved or null if timeout
535
*/
536
String brpoplpush(String source, String destination, int timeout);
537
}
538
```
539
540
#### Usage Example
541
542
```java
543
// Queue operations
544
jedis.lpush("queue", "task3", "task2", "task1"); // [task1, task2, task3]
545
String task = jedis.rpop("queue"); // "task3"
546
547
// Stack operations
548
jedis.lpush("stack", "item1", "item2", "item3");
549
String item = jedis.lpop("stack"); // "item3"
550
551
// Range operations
552
List<String> items = jedis.lrange("queue", 0, -1); // All items
553
554
// Blocking operations for producer/consumer
555
List<String> result = jedis.brpop(30, "work_queue"); // Wait up to 30 seconds
556
557
// List manipulation
558
jedis.linsert("mylist", ListPosition.BEFORE, "pivot", "new_element");
559
jedis.lrem("mylist", 1, "remove_me"); // Remove first occurrence
560
```
561
562
## Set Operations
563
564
### SetCommands
565
566
Commands for Redis set data type operations.
567
568
```java { .api }
569
public interface SetCommands {
570
/**
571
* Add members to set
572
* @param key Set key
573
* @param members Members to add
574
* @return Number of new members added
575
*/
576
Long sadd(String key, String... members);
577
578
/**
579
* Get all members of set
580
* @param key Set key
581
* @return Set of all members
582
*/
583
Set<String> smembers(String key);
584
585
/**
586
* Remove members from set
587
* @param key Set key
588
* @param members Members to remove
589
* @return Number of members removed
590
*/
591
Long srem(String key, String... members);
592
593
/**
594
* Remove and return random member from set
595
* @param key Set key
596
* @return Random member or null if set is empty
597
*/
598
String spop(String key);
599
600
/**
601
* Remove and return multiple random members
602
* @param key Set key
603
* @param count Number of members to pop
604
* @return Set of popped members
605
*/
606
Set<String> spop(String key, long count);
607
608
/**
609
* Get random member without removing
610
* @param key Set key
611
* @return Random member or null if set is empty
612
*/
613
String srandmember(String key);
614
615
/**
616
* Get multiple random members
617
* @param key Set key
618
* @param count Number of members (negative allows duplicates)
619
* @return List of random members
620
*/
621
List<String> srandmember(String key, int count);
622
623
/**
624
* Check if member exists in set
625
* @param key Set key
626
* @param member Member to check
627
* @return true if member exists
628
*/
629
Boolean sismember(String key, String member);
630
631
/**
632
* Check if multiple members exist in set
633
* @param key Set key
634
* @param members Members to check
635
* @return List of boolean results for each member
636
*/
637
List<Boolean> smismember(String key, String... members);
638
639
/**
640
* Get set cardinality (number of members)
641
* @param key Set key
642
* @return Number of members in set
643
*/
644
Long scard(String key);
645
646
/**
647
* Move member from source to destination set
648
* @param srckey Source set key
649
* @param dstkey Destination set key
650
* @param member Member to move
651
* @return 1 if member moved, 0 if member doesn't exist in source
652
*/
653
Long smove(String srckey, String dstkey, String member);
654
655
/**
656
* Intersect multiple sets
657
* @param keys Set keys
658
* @return Set intersection
659
*/
660
Set<String> sinter(String... keys);
661
662
/**
663
* Store intersection of sets in destination key
664
* @param dstkey Destination key
665
* @param keys Source set keys
666
* @return Number of members in resulting set
667
*/
668
Long sinterstore(String dstkey, String... keys);
669
670
/**
671
* Union multiple sets
672
* @param keys Set keys
673
* @return Set union
674
*/
675
Set<String> sunion(String... keys);
676
677
/**
678
* Store union of sets in destination key
679
* @param dstkey Destination key
680
* @param keys Source set keys
681
* @return Number of members in resulting set
682
*/
683
Long sunionstore(String dstkey, String... keys);
684
685
/**
686
* Difference of sets (first set minus others)
687
* @param keys Set keys
688
* @return Set difference
689
*/
690
Set<String> sdiff(String... keys);
691
692
/**
693
* Store difference of sets in destination key
694
* @param dstkey Destination key
695
* @param keys Source set keys
696
* @return Number of members in resulting set
697
*/
698
Long sdiffstore(String dstkey, String... keys);
699
700
/**
701
* Scan set members
702
* @param key Set key
703
* @param cursor Scan cursor
704
* @return Scan result with cursor and members
705
*/
706
ScanResult<String> sscan(String key, String cursor);
707
708
/**
709
* Scan set with parameters
710
* @param key Set key
711
* @param cursor Scan cursor
712
* @param params Scan parameters (MATCH, COUNT)
713
* @return Scan result with cursor and members
714
*/
715
ScanResult<String> sscan(String key, String cursor, ScanParams params);
716
}
717
```
718
719
#### Usage Example
720
721
```java
722
// Tags system
723
jedis.sadd("user:1:tags", "java", "redis", "programming");
724
jedis.sadd("user:2:tags", "python", "redis", "data-science");
725
726
// Check membership
727
boolean hasTag = jedis.sismember("user:1:tags", "redis"); // true
728
729
// Set operations
730
Set<String> commonTags = jedis.sinter("user:1:tags", "user:2:tags"); // {"redis"}
731
Set<String> allTags = jedis.sunion("user:1:tags", "user:2:tags");
732
733
// Random operations
734
String randomTag = jedis.srandmember("user:1:tags");
735
Set<String> sample = jedis.spop("temp_set", 3);
736
```
737
738
## Sorted Set Operations
739
740
### SortedSetCommands
741
742
Commands for Redis sorted set (zset) data type operations.
743
744
```java { .api }
745
public interface SortedSetCommands {
746
/**
747
* Add members with scores to sorted set
748
* @param key Sorted set key
749
* @param score Member score
750
* @param member Member value
751
* @return Number of new members added
752
*/
753
Long zadd(String key, double score, String member);
754
755
/**
756
* Add multiple members with scores
757
* @param key Sorted set key
758
* @param scoreMembers Map of scores to members
759
* @return Number of new members added
760
*/
761
Long zadd(String key, Map<String, Double> scoreMembers);
762
763
/**
764
* Add members with parameters
765
* @param key Sorted set key
766
* @param scoreMembers Score-member pairs
767
* @param params ZADD parameters (NX, XX, CH, INCR)
768
* @return Number affected or updated score if INCR used
769
*/
770
Long zadd(String key, Map<String, Double> scoreMembers, ZAddParams params);
771
772
/**
773
* Get range of members by index
774
* @param key Sorted set key
775
* @param start Start index (0-based)
776
* @param stop Stop index (-1 for end)
777
* @return List of members in range
778
*/
779
List<String> zrange(String key, long start, long stop);
780
781
/**
782
* Get range with scores
783
* @param key Sorted set key
784
* @param start Start index
785
* @param stop Stop index
786
* @return List of members with scores
787
*/
788
List<Tuple> zrangeWithScores(String key, long start, long stop);
789
790
/**
791
* Get reverse range by index
792
* @param key Sorted set key
793
* @param start Start index
794
* @param stop Stop index
795
* @return List of members in reverse order
796
*/
797
List<String> zrevrange(String key, long start, long stop);
798
799
/**
800
* Get reverse range with scores
801
* @param key Sorted set key
802
* @param start Start index
803
* @param stop Stop index
804
* @return List of members with scores in reverse order
805
*/
806
List<Tuple> zrevrangeWithScores(String key, long start, long stop);
807
808
/**
809
* Get range by score
810
* @param key Sorted set key
811
* @param min Minimum score (inclusive)
812
* @param max Maximum score (inclusive)
813
* @return List of members in score range
814
*/
815
List<String> zrangeByScore(String key, double min, double max);
816
817
/**
818
* Get range by score with limit
819
* @param key Sorted set key
820
* @param min Minimum score
821
* @param max Maximum score
822
* @param offset Result offset
823
* @param count Result count limit
824
* @return List of members in score range
825
*/
826
List<String> zrangeByScore(String key, double min, double max, int offset, int count);
827
828
/**
829
* Get range by score with scores
830
* @param key Sorted set key
831
* @param min Minimum score
832
* @param max Maximum score
833
* @return List of members with scores
834
*/
835
List<Tuple> zrangeByScoreWithScores(String key, double min, double max);
836
837
/**
838
* Get member rank (0-based index)
839
* @param key Sorted set key
840
* @param member Member value
841
* @return Rank of member or null if not found
842
*/
843
Long zrank(String key, String member);
844
845
/**
846
* Get member reverse rank
847
* @param key Sorted set key
848
* @param member Member value
849
* @return Reverse rank of member or null if not found
850
*/
851
Long zrevrank(String key, String member);
852
853
/**
854
* Get member score
855
* @param key Sorted set key
856
* @param member Member value
857
* @return Score of member or null if not found
858
*/
859
Double zscore(String key, String member);
860
861
/**
862
* Get multiple member scores
863
* @param key Sorted set key
864
* @param members Member values
865
* @return List of scores (null for non-existent members)
866
*/
867
List<Double> zmscore(String key, String... members);
868
869
/**
870
* Remove members from sorted set
871
* @param key Sorted set key
872
* @param members Members to remove
873
* @return Number of members removed
874
*/
875
Long zrem(String key, String... members);
876
877
/**
878
* Remove members by rank range
879
* @param key Sorted set key
880
* @param start Start rank (inclusive)
881
* @param stop Stop rank (inclusive)
882
* @return Number of members removed
883
*/
884
Long zremrangeByRank(String key, long start, long stop);
885
886
/**
887
* Remove members by score range
888
* @param key Sorted set key
889
* @param min Minimum score (inclusive)
890
* @param max Maximum score (inclusive)
891
* @return Number of members removed
892
*/
893
Long zremrangeByScore(String key, double min, double max);
894
895
/**
896
* Get cardinality of sorted set
897
* @param key Sorted set key
898
* @return Number of members
899
*/
900
Long zcard(String key);
901
902
/**
903
* Count members in score range
904
* @param key Sorted set key
905
* @param min Minimum score
906
* @param max Maximum score
907
* @return Number of members in range
908
*/
909
Long zcount(String key, double min, double max);
910
911
/**
912
* Increment member score
913
* @param key Sorted set key
914
* @param increment Score increment
915
* @param member Member value
916
* @return New score of member
917
*/
918
Double zincrby(String key, double increment, String member);
919
920
/**
921
* Union multiple sorted sets and store result
922
* @param dstkey Destination key
923
* @param sets Source sorted set keys
924
* @return Number of members in resulting set
925
*/
926
Long zunionstore(String dstkey, String... sets);
927
928
/**
929
* Union with parameters
930
* @param dstkey Destination key
931
* @param params Union parameters (weights, aggregate function)
932
* @param sets Source sorted set keys
933
* @return Number of members in resulting set
934
*/
935
Long zunionstore(String dstkey, ZParams params, String... sets);
936
937
/**
938
* Intersect multiple sorted sets and store result
939
* @param dstkey Destination key
940
* @param sets Source sorted set keys
941
* @return Number of members in resulting set
942
*/
943
Long zinterstore(String dstkey, String... sets);
944
945
/**
946
* Intersect with parameters
947
* @param dstkey Destination key
948
* @param params Intersection parameters
949
* @param sets Source sorted set keys
950
* @return Number of members in resulting set
951
*/
952
Long zinterstore(String dstkey, ZParams params, String... sets);
953
954
/**
955
* Scan sorted set members
956
* @param key Sorted set key
957
* @param cursor Scan cursor
958
* @return Scan result with cursor and members with scores
959
*/
960
ScanResult<Tuple> zscan(String key, String cursor);
961
}
962
```
963
964
#### Usage Example
965
966
```java
967
// Leaderboard
968
jedis.zadd("leaderboard", 1000, "player1");
969
jedis.zadd("leaderboard", 1500, "player2");
970
jedis.zadd("leaderboard", 800, "player3");
971
972
// Get top players
973
List<String> topPlayers = jedis.zrevrange("leaderboard", 0, 2);
974
975
// Get player rank
976
Long rank = jedis.zrevrank("leaderboard", "player1"); // 1 (0-based)
977
978
// Get score
979
Double score = jedis.zscore("leaderboard", "player1"); // 1000.0
980
981
// Range by score
982
List<String> midRange = jedis.zrangeByScore("leaderboard", 900, 1200);
983
984
// Increment score
985
jedis.zincrby("leaderboard", 100, "player1"); // New score: 1100
986
987
// Time-based scoring (timestamps)
988
long timestamp = System.currentTimeMillis();
989
jedis.zadd("recent_activity", timestamp, "user:123");
990
```
991
992
## Key Management Operations
993
994
### KeyCommands
995
996
Generic key management operations that work with any Redis data type.
997
998
```java { .api }
999
public interface KeyCommands {
1000
/**
1001
* Check if key exists
1002
* @param key Redis key
1003
* @return true if key exists
1004
*/
1005
Boolean exists(String key);
1006
1007
/**
1008
* Check if multiple keys exist
1009
* @param keys Redis keys
1010
* @return Number of existing keys
1011
*/
1012
Long exists(String... keys);
1013
1014
/**
1015
* Delete keys
1016
* @param keys Keys to delete
1017
* @return Number of keys deleted
1018
*/
1019
Long del(String... keys);
1020
1021
/**
1022
* Unlink keys (non-blocking delete)
1023
* @param keys Keys to unlink
1024
* @return Number of keys unlinked
1025
*/
1026
Long unlink(String... keys);
1027
1028
/**
1029
* Get key type
1030
* @param key Redis key
1031
* @return Key type (string, list, set, zset, hash, stream)
1032
*/
1033
String type(String key);
1034
1035
/**
1036
* Set key expiration in seconds
1037
* @param key Redis key
1038
* @param seconds Expiration time in seconds
1039
* @return 1 if timeout set, 0 if key doesn't exist
1040
*/
1041
Long expire(String key, long seconds);
1042
1043
/**
1044
* Set key expiration at Unix timestamp
1045
* @param key Redis key
1046
* @param unixTime Unix timestamp in seconds
1047
* @return 1 if timeout set, 0 if key doesn't exist
1048
*/
1049
Long expireAt(String key, long unixTime);
1050
1051
/**
1052
* Set key expiration in milliseconds
1053
* @param key Redis key
1054
* @param milliseconds Expiration time in milliseconds
1055
* @return 1 if timeout set, 0 if key doesn't exist
1056
*/
1057
Long pexpire(String key, long milliseconds);
1058
1059
/**
1060
* Set key expiration at Unix timestamp in milliseconds
1061
* @param key Redis key
1062
* @param millisecondsTimestamp Unix timestamp in milliseconds
1063
* @return 1 if timeout set, 0 if key doesn't exist
1064
*/
1065
Long pexpireAt(String key, long millisecondsTimestamp);
1066
1067
/**
1068
* Get time to live in seconds
1069
* @param key Redis key
1070
* @return TTL in seconds, -1 if no expiry, -2 if key doesn't exist
1071
*/
1072
Long ttl(String key);
1073
1074
/**
1075
* Get time to live in milliseconds
1076
* @param key Redis key
1077
* @return TTL in milliseconds, -1 if no expiry, -2 if key doesn't exist
1078
*/
1079
Long pttl(String key);
1080
1081
/**
1082
* Remove expiration from key
1083
* @param key Redis key
1084
* @return 1 if expiration removed, 0 if key has no expiry or doesn't exist
1085
*/
1086
Long persist(String key);
1087
1088
/**
1089
* Rename key
1090
* @param oldkey Current key name
1091
* @param newkey New key name
1092
* @return Status code reply
1093
*/
1094
String rename(String oldkey, String newkey);
1095
1096
/**
1097
* Rename key only if new key doesn't exist
1098
* @param oldkey Current key name
1099
* @param newkey New key name
1100
* @return 1 if key renamed, 0 if new key exists
1101
*/
1102
Long renamenx(String oldkey, String newkey);
1103
1104
/**
1105
* Get random key from keyspace
1106
* @return Random key or null if database is empty
1107
*/
1108
String randomKey();
1109
1110
/**
1111
* Move key to different database
1112
* @param key Redis key
1113
* @param dbIndex Target database index
1114
* @return 1 if key moved, 0 if key doesn't exist or target exists
1115
*/
1116
Long move(String key, int dbIndex);
1117
1118
/**
1119
* Find keys matching pattern
1120
* @param pattern Key pattern (glob-style)
1121
* @return Set of matching keys
1122
*/
1123
Set<String> keys(String pattern);
1124
1125
/**
1126
* Scan keyspace
1127
* @param cursor Scan cursor
1128
* @return Scan result with cursor and keys
1129
*/
1130
ScanResult<String> scan(String cursor);
1131
1132
/**
1133
* Scan keyspace with parameters
1134
* @param cursor Scan cursor
1135
* @param params Scan parameters (MATCH, COUNT, TYPE)
1136
* @return Scan result with cursor and keys
1137
*/
1138
ScanResult<String> scan(String cursor, ScanParams params);
1139
1140
/**
1141
* Sort list, set, or sorted set
1142
* @param key Key to sort
1143
* @return Sorted list of elements
1144
*/
1145
List<String> sort(String key);
1146
1147
/**
1148
* Sort with parameters
1149
* @param key Key to sort
1150
* @param sortingParams Sorting parameters (BY, LIMIT, GET, etc.)
1151
* @return Sorted list of elements
1152
*/
1153
List<String> sort(String key, SortingParams sortingParams);
1154
1155
/**
1156
* Sort and store result
1157
* @param key Key to sort
1158
* @param dstkey Destination key for sorted result
1159
* @return Number of elements in sorted result
1160
*/
1161
Long sort(String key, String dstkey);
1162
1163
/**
1164
* Get object encoding of key
1165
* @param key Redis key
1166
* @return Encoding type
1167
*/
1168
String objectEncoding(String key);
1169
1170
/**
1171
* Get object idle time
1172
* @param key Redis key
1173
* @return Idle time in seconds
1174
*/
1175
Long objectIdletime(String key);
1176
1177
/**
1178
* Get object reference count
1179
* @param key Redis key
1180
* @return Reference count
1181
*/
1182
Long objectRefcount(String key);
1183
}
1184
```
1185
1186
#### Usage Example
1187
1188
```java
1189
// Key lifecycle management
1190
jedis.set("session:abc", "userdata");
1191
jedis.expire("session:abc", 3600); // Expire in 1 hour
1192
1193
Long ttl = jedis.ttl("session:abc"); // Time to live
1194
1195
// Key discovery (use SCAN in production instead of KEYS)
1196
ScanResult<String> result = jedis.scan("0", new ScanParams().match("user:*").count(100));
1197
1198
// Key information
1199
String type = jedis.type("mykey");
1200
String encoding = jedis.objectEncoding("mykey");
1201
Long idleTime = jedis.objectIdletime("mykey");
1202
1203
// Atomic rename
1204
jedis.rename("old_key", "new_key");
1205
```
1206
1207
## Geospatial Operations
1208
1209
### GeoCommands
1210
1211
Commands for Redis geospatial operations using sorted sets.
1212
1213
```java { .api }
1214
public interface GeoCommands {
1215
/**
1216
* Add geospatial members with coordinates
1217
* @param key Geo set key
1218
* @param longitude Longitude coordinate
1219
* @param latitude Latitude coordinate
1220
* @param member Member name
1221
* @return Number of new members added
1222
*/
1223
Long geoadd(String key, double longitude, double latitude, String member);
1224
1225
/**
1226
* Add multiple geospatial members
1227
* @param key Geo set key
1228
* @param memberCoordinateMap Map of members to coordinates
1229
* @return Number of new members added
1230
*/
1231
Long geoadd(String key, Map<String, GeoCoordinate> memberCoordinateMap);
1232
1233
/**
1234
* Add geospatial members with parameters
1235
* @param key Geo set key
1236
* @param params GEOADD parameters (NX, XX)
1237
* @param memberCoordinateMap Map of members to coordinates
1238
* @return Number affected (added or updated)
1239
*/
1240
Long geoadd(String key, GeoAddParams params, Map<String, GeoCoordinate> memberCoordinateMap);
1241
1242
/**
1243
* Get distance between two members
1244
* @param key Geo set key
1245
* @param member1 First member
1246
* @param member2 Second member
1247
* @return Distance in meters
1248
*/
1249
Double geodist(String key, String member1, String member2);
1250
1251
/**
1252
* Get distance with unit
1253
* @param key Geo set key
1254
* @param member1 First member
1255
* @param member2 Second member
1256
* @param unit Distance unit (M, KM, MI, FT)
1257
* @return Distance in specified unit
1258
*/
1259
Double geodist(String key, String member1, String member2, GeoUnit unit);
1260
1261
/**
1262
* Get geohash strings for members
1263
* @param key Geo set key
1264
* @param members Member names
1265
* @return List of geohash strings
1266
*/
1267
List<String> geohash(String key, String... members);
1268
1269
/**
1270
* Get coordinates for members
1271
* @param key Geo set key
1272
* @param members Member names
1273
* @return List of coordinates (null for non-existent members)
1274
*/
1275
List<GeoCoordinate> geopos(String key, String... members);
1276
1277
/**
1278
* Find members within radius of coordinates
1279
* @param key Geo set key
1280
* @param longitude Center longitude
1281
* @param latitude Center latitude
1282
* @param radius Search radius
1283
* @param unit Radius unit
1284
* @return List of members within radius
1285
*/
1286
List<GeoRadiusResponse> georadius(String key, double longitude, double latitude,
1287
double radius, GeoUnit unit);
1288
1289
/**
1290
* Find members within radius with parameters
1291
* @param key Geo set key
1292
* @param longitude Center longitude
1293
* @param latitude Center latitude
1294
* @param radius Search radius
1295
* @param unit Radius unit
1296
* @param param Query parameters (WITHCOORD, WITHDIST, COUNT, etc.)
1297
* @return List of members with requested information
1298
*/
1299
List<GeoRadiusResponse> georadius(String key, double longitude, double latitude,
1300
double radius, GeoUnit unit, GeoRadiusParam param);
1301
1302
/**
1303
* Find members within radius of existing member
1304
* @param key Geo set key
1305
* @param member Center member
1306
* @param radius Search radius
1307
* @param unit Radius unit
1308
* @return List of members within radius
1309
*/
1310
List<GeoRadiusResponse> georadiusByMember(String key, String member,
1311
double radius, GeoUnit unit);
1312
1313
/**
1314
* Find members within radius of member with parameters
1315
* @param key Geo set key
1316
* @param member Center member
1317
* @param radius Search radius
1318
* @param unit Radius unit
1319
* @param param Query parameters
1320
* @return List of members with requested information
1321
*/
1322
List<GeoRadiusResponse> georadiusByMember(String key, String member,
1323
double radius, GeoUnit unit,
1324
GeoRadiusParam param);
1325
1326
/**
1327
* Search members by distance or box
1328
* @param key Geo set key
1329
* @param params Search parameters (BY RADIUS/BOX, FROM MEMBER/LONLAT, etc.)
1330
* @return List of matching members
1331
*/
1332
List<GeoRadiusResponse> geosearch(String key, GeoSearchParam params);
1333
1334
/**
1335
* Search and store results in destination key
1336
* @param dest Destination key
1337
* @param src Source geo set key
1338
* @param params Search parameters
1339
* @return Number of members stored
1340
*/
1341
Long geosearchStore(String dest, String src, GeoSearchParam params);
1342
}
1343
```
1344
1345
#### Usage Example
1346
1347
```java
1348
// Add locations
1349
jedis.geoadd("cities", -74.0059, 40.7128, "New York");
1350
jedis.geoadd("cities", -118.2437, 34.0522, "Los Angeles");
1351
jedis.geoadd("cities", -87.6298, 41.8781, "Chicago");
1352
1353
// Get distance between cities
1354
Double distance = jedis.geodist("cities", "New York", "Chicago", GeoUnit.MI);
1355
1356
// Find cities within radius
1357
List<GeoRadiusResponse> nearby = jedis.georadius("cities", -74.0, 40.7,
1358
500, GeoUnit.MI,
1359
GeoRadiusParam.geoRadiusParam().withDist().withCoord().sortAscending()
1360
);
1361
1362
for (GeoRadiusResponse city : nearby) {
1363
System.out.println(city.getMemberByString() + " - " +
1364
city.getDistance() + " miles");
1365
}
1366
1367
// Get coordinates
1368
List<GeoCoordinate> coords = jedis.geopos("cities", "New York", "Chicago");
1369
```
1370
1371
This comprehensive commands documentation covers the major Redis data types and operations. Each interface provides type-safe access to Redis functionality with proper error handling and return types that match Redis server responses.