0
# Core Client Classes
1
2
This document covers the primary Jedis client classes for different Redis deployment patterns and use cases.
3
4
## Primary Client Classes
5
6
### Jedis
7
8
The main synchronous Redis client implementing all Redis commands.
9
10
```java { .api }
11
public class Jedis implements ServerCommands, DatabaseCommands, JedisCommands, JedisBinaryCommands,
12
ControlCommands, ControlBinaryCommands, ClusterCommands, ModuleCommands, GenericControlCommands,
13
SentinelCommands, CommandCommands, Closeable {
14
/**
15
* Creates a new Jedis client connected to localhost:6379
16
*/
17
public Jedis();
18
19
/**
20
* Creates a new Jedis client from URL string
21
* @param url Redis connection URL
22
*/
23
public Jedis(String url);
24
25
/**
26
* Creates a new Jedis client
27
* @param hp Host and port endpoint
28
*/
29
public Jedis(HostAndPort hp);
30
31
/**
32
* Creates a new Jedis client
33
* @param host Redis server host
34
* @param port Redis server port
35
*/
36
public Jedis(String host, int port);
37
38
/**
39
* Creates a new Jedis client with configuration
40
* @param host Redis server host
41
* @param port Redis server port
42
* @param config Client configuration
43
*/
44
public Jedis(String host, int port, JedisClientConfig config);
45
46
/**
47
* Creates a new Jedis client with endpoint and configuration
48
* @param hostPort Host and port endpoint
49
* @param config Client configuration
50
*/
51
public Jedis(HostAndPort hostPort, JedisClientConfig config);
52
53
/**
54
* Creates a new Jedis client with SSL support
55
* @param host Redis server host
56
* @param port Redis server port
57
* @param ssl Enable SSL connection
58
*/
59
public Jedis(String host, int port, boolean ssl);
60
61
/**
62
* Creates a Jedis client with timeout
63
* @param host Redis server host
64
* @param port Redis server port
65
* @param timeout Connection and socket timeout in milliseconds
66
*/
67
public Jedis(String host, int port, int timeout);
68
69
/**
70
* Creates a Jedis client with timeout and SSL
71
* @param host Redis server host
72
* @param port Redis server port
73
* @param timeout Connection and socket timeout in milliseconds
74
* @param ssl Enable SSL connection
75
*/
76
public Jedis(String host, int port, int timeout, boolean ssl);
77
78
/**
79
* Creates a Jedis client with connection details
80
* @param host Redis server host
81
* @param port Redis server port
82
* @param connectionTimeout Connection timeout in milliseconds
83
* @param soTimeout Socket timeout in milliseconds
84
*/
85
public Jedis(String host, int port, int connectionTimeout, int soTimeout);
86
87
/**
88
* Creates a Jedis client with timeouts and SSL
89
* @param host Redis server host
90
* @param port Redis server port
91
* @param connectionTimeout Connection timeout in milliseconds
92
* @param soTimeout Socket timeout in milliseconds
93
* @param ssl Enable SSL connection
94
*/
95
public Jedis(String host, int port, int connectionTimeout, int soTimeout, boolean ssl);
96
97
/**
98
* Creates a new Jedis client from URI
99
* @param uri Redis connection URI (redis://host:port/db)
100
*/
101
public Jedis(URI uri);
102
103
/**
104
* Creates a new Jedis client from URI with timeout
105
* @param uri Redis connection URI
106
* @param timeout Connection timeout in milliseconds
107
*/
108
public Jedis(URI uri, int timeout);
109
110
/**
111
* Creates a new Jedis client from URI with timeouts
112
* @param uri Redis connection URI
113
* @param connectionTimeout Connection timeout in milliseconds
114
* @param soTimeout Socket timeout in milliseconds
115
*/
116
public Jedis(URI uri, int connectionTimeout, int soTimeout);
117
118
/**
119
* Creates a new Jedis client from URI with configuration
120
* @param uri Redis connection URI
121
* @param config Client configuration
122
*/
123
public Jedis(URI uri, JedisClientConfig config);
124
125
/**
126
* Creates a new Jedis client with socket factory
127
* @param jedisSocketFactory Custom socket factory
128
*/
129
public Jedis(JedisSocketFactory jedisSocketFactory);
130
131
/**
132
* Creates a new Jedis client with socket factory and configuration
133
* @param jedisSocketFactory Custom socket factory
134
* @param clientConfig Client configuration
135
*/
136
public Jedis(JedisSocketFactory jedisSocketFactory, JedisClientConfig clientConfig);
137
138
/**
139
* Creates a new Jedis client with existing connection
140
* @param connection Existing connection object
141
*/
142
public Jedis(Connection connection);
143
144
/**
145
* Tests if client is connected to Redis server
146
* @return true if connected
147
*/
148
public boolean isConnected();
149
150
/**
151
* Gets the database index currently selected
152
* @return Database index (0-15)
153
*/
154
public int getDB();
155
156
/**
157
* Executes Redis AUTH command for authentication
158
* @param password Redis password
159
* @return Status code reply
160
*/
161
public String auth(String password);
162
163
/**
164
* Executes Redis AUTH command with username and password
165
* @param user Redis username
166
* @param password Redis password
167
* @return Status code reply
168
*/
169
public String auth(String user, String password);
170
171
/**
172
* Selects a Redis database
173
* @param index Database index (0-15)
174
* @return Status code reply
175
*/
176
public String select(int index);
177
178
/**
179
* Closes the connection to Redis server
180
*/
181
public void close();
182
183
/**
184
* Returns connection to Redis server
185
* @return Connection object
186
*/
187
public Connection getConnection();
188
}
189
```
190
191
#### Usage Example
192
193
```java
194
// Basic usage
195
Jedis jedis = new Jedis("localhost", 6379);
196
jedis.set("key", "value");
197
String value = jedis.get("key");
198
jedis.close();
199
200
// With authentication
201
Jedis jedis = new Jedis("localhost", 6379);
202
jedis.auth("password");
203
jedis.select(1); // Use database 1
204
jedis.set("key", "value");
205
jedis.close();
206
207
// With URI and SSL
208
URI uri = URI.create("rediss://username:password@redis.example.com:6380/0");
209
JedisClientConfig config = DefaultJedisClientConfig.builder()
210
.ssl(true)
211
.sslVerifyMode(SslVerifyMode.FULL)
212
.build();
213
Jedis jedis = new Jedis(uri, config);
214
```
215
216
### UnifiedJedis
217
218
Unified client interface supporting multiple deployment modes (single, cluster, sentinel).
219
220
```java { .api }
221
public class UnifiedJedis implements JedisCommands, JedisBinaryCommands,
222
SampleKeyedCommands, SampleBinaryKeyedCommands, RedisModuleCommands,
223
AutoCloseable {
224
/**
225
* Creates UnifiedJedis with default localhost:6379 connection
226
*/
227
public UnifiedJedis();
228
229
/**
230
* Creates UnifiedJedis for single Redis instance
231
* @param hostAndPort Host and port endpoint
232
*/
233
public UnifiedJedis(HostAndPort hostAndPort);
234
235
/**
236
* Creates UnifiedJedis from URL string
237
* @param url Redis connection URL
238
*/
239
public UnifiedJedis(String url);
240
241
/**
242
* Creates UnifiedJedis from Redis URI
243
* @param uri Redis connection URI
244
*/
245
public UnifiedJedis(URI uri);
246
247
/**
248
* Creates UnifiedJedis from URI with configuration
249
* @param uri Redis connection URI
250
* @param config Client configuration
251
*/
252
public UnifiedJedis(URI uri, JedisClientConfig config);
253
254
/**
255
* Creates UnifiedJedis for single instance with configuration
256
* @param hostAndPort Host and port endpoint
257
* @param clientConfig Client configuration
258
*/
259
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig);
260
261
/**
262
* Creates UnifiedJedis with endpoint, client config and cache config
263
* @param hostAndPort Host and port endpoint
264
* @param clientConfig Client configuration
265
* @param cacheConfig Cache configuration
266
*/
267
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, CacheConfig cacheConfig);
268
269
/**
270
* Creates UnifiedJedis with endpoint, client config and cache
271
* @param hostAndPort Host and port endpoint
272
* @param clientConfig Client configuration
273
* @param cache Cache instance
274
*/
275
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, Cache cache);
276
277
/**
278
* Creates UnifiedJedis with connection provider
279
* @param provider Connection provider for different deployment types
280
*/
281
public UnifiedJedis(ConnectionProvider provider);
282
283
/**
284
* Creates UnifiedJedis with socket factory
285
* @param socketFactory Custom socket factory
286
*/
287
public UnifiedJedis(JedisSocketFactory socketFactory);
288
289
/**
290
* Creates UnifiedJedis with socket factory and configuration
291
* @param socketFactory Custom socket factory
292
* @param clientConfig Client configuration
293
*/
294
public UnifiedJedis(JedisSocketFactory socketFactory, JedisClientConfig clientConfig);
295
296
/**
297
* Creates UnifiedJedis with existing connection
298
* @param connection Existing connection object
299
*/
300
public UnifiedJedis(Connection connection);
301
302
/**
303
* Creates UnifiedJedis for Redis Cluster
304
* @param jedisClusterNodes Set of cluster node endpoints
305
* @param clientConfig Client configuration
306
* @param maxAttempts Maximum retry attempts
307
*/
308
public UnifiedJedis(Set<HostAndPort> jedisClusterNodes, JedisClientConfig clientConfig, int maxAttempts);
309
310
/**
311
* Creates UnifiedJedis with cluster provider and retry settings
312
* @param provider Cluster connection provider
313
* @param maxAttempts Maximum retry attempts
314
* @param maxTotalRetriesDuration Maximum total retry duration
315
*/
316
public UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration);
317
318
/**
319
* Creates UnifiedJedis with sharded provider
320
* @param provider Sharded connection provider
321
*/
322
public UnifiedJedis(ShardedConnectionProvider provider);
323
324
/**
325
* Creates UnifiedJedis with sharded provider and tag pattern
326
* @param provider Sharded connection provider
327
* @param tagPattern Tag pattern for key routing
328
*/
329
public UnifiedJedis(ShardedConnectionProvider provider, Pattern tagPattern);
330
331
/**
332
* Creates UnifiedJedis with connection provider and retry settings
333
* @param provider Connection provider
334
* @param maxAttempts Maximum retry attempts
335
* @param maxTotalRetriesDuration Maximum total retry duration
336
*/
337
public UnifiedJedis(ConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration);
338
339
/**
340
* Creates UnifiedJedis with multi-cluster provider
341
* @param provider Multi-cluster connection provider
342
*/
343
public UnifiedJedis(MultiClusterPooledConnectionProvider provider);
344
345
/**
346
* Creates UnifiedJedis with command executor
347
* @param executor Command executor
348
*/
349
public UnifiedJedis(CommandExecutor executor);
350
351
/**
352
* Creates UnifiedJedis with executor, provider and command objects
353
* @param executor Command executor
354
* @param provider Connection provider
355
* @param commandObjects Command objects factory
356
*/
357
public UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects);
358
359
/**
360
* Closes the client and releases resources
361
*/
362
public void close();
363
}
364
```
365
366
#### Usage Example
367
368
```java
369
// Single instance
370
UnifiedJedis jedis = new UnifiedJedis("localhost", 6379);
371
372
// Cluster deployment
373
Set<HostAndPort> clusterNodes = Set.of(
374
new HostAndPort("redis-node1", 7000),
375
new HostAndPort("redis-node2", 7001),
376
new HostAndPort("redis-node3", 7002)
377
);
378
UnifiedJedis jedis = new UnifiedJedis(clusterNodes);
379
380
// With connection provider
381
ConnectionProvider provider = new PooledConnectionProvider(
382
new JedisPool("localhost", 6379)
383
);
384
UnifiedJedis jedis = new UnifiedJedis(provider);
385
```
386
387
### JedisCluster
388
389
Specialized client for Redis Cluster deployments with automatic sharding and failover.
390
391
```java { .api }
392
public class JedisCluster extends UnifiedJedis {
393
/**
394
* Creates cluster client with default configuration
395
* @param jedisClusterNode Single cluster node endpoint
396
*/
397
public JedisCluster(HostAndPort jedisClusterNode);
398
399
/**
400
* Creates cluster client
401
* @param clusterNodes Set of cluster node endpoints
402
*/
403
public JedisCluster(Set<HostAndPort> clusterNodes);
404
405
/**
406
* Creates cluster client with configuration
407
* @param clusterNodes Set of cluster node endpoints
408
* @param config Client configuration
409
*/
410
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig config);
411
412
/**
413
* Creates cluster client with timeout configuration
414
* @param clusterNodes Set of cluster node endpoints
415
* @param connectionTimeout Connection timeout in milliseconds
416
* @param soTimeout Socket timeout in milliseconds
417
*/
418
public JedisCluster(Set<HostAndPort> clusterNodes, int connectionTimeout, int soTimeout);
419
420
/**
421
* Creates cluster client with pool configuration
422
* @param clusterNodes Set of cluster node endpoints
423
* @param config Client configuration
424
* @param poolConfig Pool configuration
425
*/
426
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig config,
427
JedisPoolConfig poolConfig);
428
429
/**
430
* Gets cluster information
431
* @return Cluster info string
432
*/
433
public String clusterInfo();
434
435
/**
436
* Gets cluster nodes information
437
* @return Cluster nodes info
438
*/
439
public String clusterNodes();
440
441
/**
442
* Gets cluster slots mapping
443
* @return List of cluster slot ranges and responsible nodes
444
*/
445
public List<Object> clusterSlots();
446
447
/**
448
* Refreshes cluster slot mapping
449
*/
450
public void refreshClusterSlots();
451
452
/**
453
* Gets connection pool for specific cluster node
454
* @param node Cluster node endpoint
455
* @return Connection pool for the node
456
*/
457
public JedisPool getConnectionPoolFromSlot(int slot);
458
459
/**
460
* Gets all connection pools in cluster
461
* @return Map of node endpoints to connection pools
462
*/
463
public Map<String, JedisPool> getClusterNodes();
464
465
/**
466
* Closes cluster client and all connections
467
*/
468
public void close();
469
}
470
```
471
472
#### Usage Example
473
474
```java
475
Set<HostAndPort> clusterNodes = new HashSet<>();
476
clusterNodes.add(new HostAndPort("redis-node1.example.com", 7000));
477
clusterNodes.add(new HostAndPort("redis-node2.example.com", 7001));
478
clusterNodes.add(new HostAndPort("redis-node3.example.com", 7002));
479
480
JedisCluster cluster = new JedisCluster(clusterNodes);
481
482
// Use like regular Redis client - clustering is transparent
483
cluster.set("user:1:name", "John");
484
cluster.hset("user:1:profile", "age", "30");
485
String name = cluster.get("user:1:name");
486
487
// Cluster-specific operations
488
String clusterInfo = cluster.clusterInfo();
489
String nodes = cluster.clusterNodes();
490
491
cluster.close();
492
```
493
494
## Pooled Client Classes
495
496
### JedisPool
497
498
Thread-safe connection pool for single Redis instance.
499
500
```java { .api }
501
public class JedisPool implements Pool<Jedis>, Closeable {
502
/**
503
* Creates pool with default configuration
504
* @param host Redis server host
505
* @param port Redis server port
506
*/
507
public JedisPool(String host, int port);
508
509
/**
510
* Creates pool with pool configuration
511
* @param poolConfig Pool configuration settings
512
* @param host Redis server host
513
* @param port Redis server port
514
*/
515
public JedisPool(JedisPoolConfig poolConfig, String host, int port);
516
517
/**
518
* Creates pool with client configuration
519
* @param poolConfig Pool configuration
520
* @param host Redis server host
521
* @param port Redis server port
522
* @param clientConfig Client configuration
523
*/
524
public JedisPool(JedisPoolConfig poolConfig, String host, int port,
525
JedisClientConfig clientConfig);
526
527
/**
528
* Creates pool from URI
529
* @param uri Redis connection URI
530
*/
531
public JedisPool(URI uri);
532
533
/**
534
* Creates pool from URI with configuration
535
* @param poolConfig Pool configuration
536
* @param uri Redis connection URI
537
*/
538
public JedisPool(JedisPoolConfig poolConfig, URI uri);
539
540
/**
541
* Gets a Jedis instance from pool
542
* @return Jedis instance (must be returned to pool after use)
543
*/
544
public Jedis getResource();
545
546
/**
547
* Gets number of active connections in pool
548
* @return Number of active connections
549
*/
550
public int getNumActive();
551
552
/**
553
* Gets number of idle connections in pool
554
* @return Number of idle connections
555
*/
556
public int getNumIdle();
557
558
/**
559
* Gets number of threads waiting for connections
560
* @return Number of waiting threads
561
*/
562
public int getNumWaiters();
563
564
/**
565
* Closes pool and all connections
566
*/
567
public void close();
568
569
/**
570
* Destroys pool (alias for close)
571
*/
572
public void destroy();
573
}
574
```
575
576
#### Usage Example
577
578
```java
579
// Create pool with configuration
580
JedisPoolConfig poolConfig = new JedisPoolConfig();
581
poolConfig.setMaxTotal(128);
582
poolConfig.setMaxIdle(128);
583
poolConfig.setMinIdle(16);
584
poolConfig.setTestOnBorrow(true);
585
586
JedisPool pool = new JedisPool(poolConfig, "localhost", 6379);
587
588
// Use pool - always use try-with-resources
589
try (Jedis jedis = pool.getResource()) {
590
jedis.set("key", "value");
591
String value = jedis.get("key");
592
}
593
594
// Monitor pool health
595
System.out.println("Active connections: " + pool.getNumActive());
596
System.out.println("Idle connections: " + pool.getNumIdle());
597
598
// Clean shutdown
599
pool.close();
600
```
601
602
### JedisSentinelPool
603
604
Connection pool with Redis Sentinel integration for high availability.
605
606
```java { .api }
607
public class JedisSentinelPool extends Pool<Jedis> {
608
/**
609
* Creates sentinel pool
610
* @param masterName Name of Redis master in Sentinel configuration
611
* @param sentinels Set of Sentinel endpoints
612
*/
613
public JedisSentinelPool(String masterName, Set<String> sentinels);
614
615
/**
616
* Creates sentinel pool with pool configuration
617
* @param masterName Name of Redis master
618
* @param sentinels Set of Sentinel endpoints
619
* @param poolConfig Pool configuration
620
*/
621
public JedisSentinelPool(String masterName, Set<String> sentinels,
622
JedisPoolConfig poolConfig);
623
624
/**
625
* Creates sentinel pool with client configuration
626
* @param masterName Name of Redis master
627
* @param sentinels Set of Sentinel endpoints
628
* @param clientConfig Client configuration
629
*/
630
public JedisSentinelPool(String masterName, Set<String> sentinels,
631
JedisClientConfig clientConfig);
632
633
/**
634
* Creates sentinel pool with full configuration
635
* @param masterName Name of Redis master
636
* @param sentinels Set of Sentinel endpoints
637
* @param poolConfig Pool configuration
638
* @param clientConfig Client configuration
639
*/
640
public JedisSentinelPool(String masterName, Set<String> sentinels,
641
JedisPoolConfig poolConfig, JedisClientConfig clientConfig);
642
643
/**
644
* Gets current master address
645
* @return Current master host and port
646
*/
647
public HostAndPort getCurrentHostMaster();
648
649
/**
650
* Gets a Jedis connection from pool
651
* @return Jedis instance
652
*/
653
public Jedis getResource();
654
655
/**
656
* Destroys the pool
657
*/
658
public void destroy();
659
}
660
```
661
662
#### Usage Example
663
664
```java
665
String masterName = "mymaster";
666
Set<String> sentinels = Set.of(
667
"sentinel1:26379",
668
"sentinel2:26379",
669
"sentinel3:26379"
670
);
671
672
JedisPoolConfig poolConfig = new JedisPoolConfig();
673
poolConfig.setMaxTotal(20);
674
675
JedisSentinelPool sentinelPool = new JedisSentinelPool(
676
masterName, sentinels, poolConfig
677
);
678
679
// Use like regular pool - failover is automatic
680
try (Jedis jedis = sentinelPool.getResource()) {
681
jedis.set("key", "value");
682
String value = jedis.get("key");
683
}
684
685
// Check current master
686
HostAndPort master = sentinelPool.getCurrentHostMaster();
687
System.out.println("Current master: " + master);
688
689
sentinelPool.destroy();
690
```
691
692
### JedisPooled
693
694
Simplified pooled client with built-in connection management.
695
696
```java { .api }
697
public class JedisPooled implements JedisCommands, JedisBinaryCommands, AutoCloseable {
698
/**
699
* Creates pooled client
700
* @param host Redis server host
701
* @param port Redis server port
702
*/
703
public JedisPooled(String host, int port);
704
705
/**
706
* Creates pooled client with configuration
707
* @param host Redis server host
708
* @param port Redis server port
709
* @param config Client configuration
710
*/
711
public JedisPooled(String host, int port, JedisClientConfig config);
712
713
/**
714
* Creates pooled client with pool configuration
715
* @param poolConfig Pool configuration
716
* @param host Redis server host
717
* @param port Redis server port
718
*/
719
public JedisPooled(JedisPoolConfig poolConfig, String host, int port);
720
721
/**
722
* Creates pooled client from URI
723
* @param uri Redis connection URI
724
*/
725
public JedisPooled(URI uri);
726
727
/**
728
* Creates pooled client from connection provider
729
* @param connectionProvider Connection provider
730
*/
731
public JedisPooled(ConnectionProvider connectionProvider);
732
733
/**
734
* Creates pooled client with no parameters (localhost:6379)
735
*/
736
public JedisPooled();
737
738
/**
739
* Creates pooled client with host and port endpoint
740
* @param hostAndPort Server endpoint
741
*/
742
public JedisPooled(HostAndPort hostAndPort);
743
744
/**
745
* Creates pooled client with endpoint and client config
746
* @param hostAndPort Server endpoint
747
* @param clientConfig Client configuration
748
*/
749
public JedisPooled(HostAndPort hostAndPort, JedisClientConfig clientConfig);
750
751
/**
752
* Creates pooled client with pool and client config
753
* @param poolConfig Pool configuration
754
* @param hostAndPort Server endpoint
755
* @param clientConfig Client configuration
756
*/
757
public JedisPooled(GenericObjectPoolConfig<Connection> poolConfig, HostAndPort hostAndPort, JedisClientConfig clientConfig);
758
759
/**
760
* Creates pooled client with caching support
761
* @param hostAndPort Server endpoint
762
* @param clientConfig Client configuration
763
* @param cacheConfig Cache configuration
764
*/
765
public JedisPooled(HostAndPort hostAndPort, JedisClientConfig clientConfig, CacheConfig cacheConfig);
766
767
/**
768
* Gets the underlying connection pool
769
* @return Connection pool
770
*/
771
public Pool<Connection> getPool();
772
773
/**
774
* Closes the pooled client
775
*/
776
public void close();
777
}
778
```
779
780
#### Usage Example
781
782
```java
783
// Simple usage - no manual connection management needed
784
JedisPooled jedis = new JedisPooled("localhost", 6379);
785
786
// Use directly like regular Jedis - pooling is transparent
787
jedis.set("key", "value");
788
String value = jedis.get("key");
789
jedis.hset("hash", "field", "value");
790
791
// No need to return connections - handled automatically
792
jedis.close();
793
```
794
795
## Sharded Clients
796
797
### JedisSharding
798
799
Client for consistent hashing across multiple Redis instances.
800
801
```java { .api }
802
public class JedisSharding implements JedisCommands, AutoCloseable {
803
/**
804
* Creates sharding client
805
* @param shards List of Redis shards with weights
806
* @param algo Hashing algorithm
807
*/
808
public JedisSharding(List<JedisShardInfo> shards, Hashing algo);
809
810
/**
811
* Creates sharding client with default hashing
812
* @param shards List of Redis shards
813
*/
814
public JedisSharding(List<JedisShardInfo> shards);
815
816
/**
817
* Gets shard info for a key
818
* @param key Redis key
819
* @return Shard information for the key
820
*/
821
public JedisShardInfo getShardInfo(String key);
822
823
/**
824
* Gets collection of all shards
825
* @return Collection of shard information
826
*/
827
public Collection<JedisShardInfo> getAllShardInfo();
828
829
/**
830
* Gets Jedis instance for a specific key
831
* @param key Redis key
832
* @return Jedis instance for the shard containing the key
833
*/
834
public Jedis getShard(String key);
835
836
/**
837
* Closes all shard connections
838
*/
839
public void close();
840
}
841
```
842
843
#### Usage Example
844
845
```java
846
List<JedisShardInfo> shards = Arrays.asList(
847
new JedisShardInfo("redis1", 6379, 1), // weight 1
848
new JedisShardInfo("redis2", 6379, 1), // weight 1
849
new JedisShardInfo("redis3", 6379, 2) // weight 2 (handles more keys)
850
);
851
852
JedisSharding sharding = new JedisSharding(shards);
853
854
// Keys are automatically distributed across shards
855
sharding.set("user:1", "data1"); // Goes to shard based on key hash
856
sharding.set("user:2", "data2"); // May go to different shard
857
858
// Get data - client finds correct shard automatically
859
String data = sharding.get("user:1");
860
861
sharding.close();
862
```
863
864
## Client Configuration Types
865
866
### JedisClientConfig
867
868
```java { .api }
869
public interface JedisClientConfig {
870
int getConnectionTimeoutMillis();
871
int getSoTimeoutMillis();
872
int getBlockingSocketTimeoutMillis();
873
String getUser();
874
String getPassword();
875
int getDatabase();
876
String getClientName();
877
boolean isSsl();
878
SslSocketFactory getSslSocketFactory();
879
SSLParameters getSslParameters();
880
HostnameVerifier getHostnameVerifier();
881
RedisProtocol getRedisProtocol();
882
}
883
884
public class DefaultJedisClientConfig implements JedisClientConfig {
885
public static Builder builder() {
886
return new Builder();
887
}
888
889
public static class Builder {
890
public Builder connectionTimeoutMillis(int connectionTimeoutMillis);
891
public Builder socketTimeoutMillis(int socketTimeoutMillis);
892
public Builder blockingSocketTimeoutMillis(int blockingSocketTimeoutMillis);
893
public Builder user(String user);
894
public Builder password(String password);
895
public Builder database(int database);
896
public Builder clientName(String clientName);
897
public Builder ssl(boolean ssl);
898
public Builder protocol(RedisProtocol protocol);
899
public DefaultJedisClientConfig build();
900
}
901
}
902
```
903
904
### JedisShardInfo
905
906
```java { .api }
907
public class JedisShardInfo {
908
/**
909
* Creates shard info
910
* @param host Redis host
911
* @param port Redis port
912
*/
913
public JedisShardInfo(String host, int port);
914
915
/**
916
* Creates shard info with name
917
* @param host Redis host
918
* @param port Redis port
919
* @param name Shard name
920
*/
921
public JedisShardInfo(String host, int port, String name);
922
923
/**
924
* Creates shard info with weight
925
* @param host Redis host
926
* @param port Redis port
927
* @param weight Shard weight for distribution
928
*/
929
public JedisShardInfo(String host, int port, int weight);
930
931
/**
932
* Creates shard info from URI
933
* @param uri Redis connection URI
934
*/
935
public JedisShardInfo(URI uri);
936
937
public String getHost();
938
public int getPort();
939
public String getName();
940
public int getWeight();
941
public int getTimeout();
942
public String getPassword();
943
}
944
```