1313import redis .clients .jedis .providers .ConnectionProvider ;
1414import redis .clients .jedis .util .JedisClusterCRC16 ;
1515
16+ /**
17+ * RedisClusterClient provides a high-level, unified interface for interacting with a Redis Cluster.
18+ * <p>
19+ * This class is intended as a modern replacement for the deprecated {@code JedisCluster} class. It
20+ * supports all cluster operations and is designed to work seamlessly with the {@link UnifiedJedis}
21+ * API, allowing for consistent usage patterns across standalone, sentinel, and cluster deployments.
22+ * <p>
23+ * <b>Usage:</b>
24+ *
25+ * <pre>
26+ * {
27+ * @code
28+ * Set<HostAndPort> clusterNodes = new HashSet<>();
29+ * clusterNodes.add(new HostAndPort("127.0.0.1", 7000));
30+ * RedisClusterClient client = new RedisClusterClient(clusterNodes);
31+ * client.set("key", "value");
32+ * String value = client.get("key");
33+ * }
34+ * </pre>
35+ * <p>
36+ * <b>Migration:</b> Users of {@code JedisCluster} are encouraged to migrate to this class for
37+ * improved API consistency, better resource management, and enhanced support for future Redis
38+ * features.
39+ * <p>
40+ * <b>Thread-safety:</b> This client is thread-safe and can be shared across multiple threads.
41+ * <p>
42+ * <b>Configuration:</b> Various constructors allow for flexible configuration, including
43+ * authentication and custom timeouts.
44+ */
1645public class RedisClusterClient extends UnifiedJedis {
1746
1847 public static final String INIT_NO_ERROR_PROPERTY = "jedis.cluster.initNoError" ;
@@ -28,35 +57,46 @@ public class RedisClusterClient extends UnifiedJedis {
2857 public static final int DEFAULT_MAX_ATTEMPTS = 5 ;
2958
3059 /**
31- * Creates a RedisClusterClient instance. The provided node is used to make the first contact with the cluster.
60+ * Creates a RedisClusterClient instance. The provided node is used to make the first contact with
61+ * the cluster.
3262 * <p>
33- * Here, the default timeout of {@value redis.clients.jedis.RedisClusterClient#DEFAULT_TIMEOUT} ms is being used with
34- * {@value redis.clients.jedis.RedisClusterClient#DEFAULT_MAX_ATTEMPTS} maximum attempts.
63+ * Here, the default timeout of {@value redis.clients.jedis.RedisClusterClient#DEFAULT_TIMEOUT} ms
64+ * is being used with {@value redis.clients.jedis.RedisClusterClient#DEFAULT_MAX_ATTEMPTS} maximum
65+ * attempts.
3566 * @param node Node to first connect to.
3667 */
3768 public RedisClusterClient (HostAndPort node ) {
38- super (new ClusterConnectionProvider (Collections .singleton (node ), DefaultJedisClientConfig .builder ().timeoutMillis (DEFAULT_TIMEOUT ).build ()),
69+ super (
70+ new ClusterConnectionProvider (Collections .singleton (node ),
71+ DefaultJedisClientConfig .builder ().timeoutMillis (DEFAULT_TIMEOUT ).build ()),
3972 DEFAULT_MAX_ATTEMPTS , Duration .ofMillis ((long ) DEFAULT_TIMEOUT * DEFAULT_MAX_ATTEMPTS ));
4073 }
4174
4275 /**
4376 * Creates a RedisClusterClient with multiple entry points.
4477 * <p>
45- * Here, the default timeout of {@value redis.clients.jedis.RedisClusterClient#DEFAULT_TIMEOUT} ms is being used with
46- * {@value redis.clients.jedis.RedisClusterClient#DEFAULT_MAX_ATTEMPTS} maximum attempts.
78+ * Here, the default timeout of {@value redis.clients.jedis.RedisClusterClient#DEFAULT_TIMEOUT} ms
79+ * is being used with {@value redis.clients.jedis.RedisClusterClient#DEFAULT_MAX_ATTEMPTS} maximum
80+ * attempts.
4781 * @param nodes Nodes to connect to.
4882 */
4983 public RedisClusterClient (Set <HostAndPort > nodes ) {
50- super (new ClusterConnectionProvider (nodes , DefaultJedisClientConfig .builder ().timeoutMillis (DEFAULT_TIMEOUT ).build ()),
84+ super (
85+ new ClusterConnectionProvider (nodes ,
86+ DefaultJedisClientConfig .builder ().timeoutMillis (DEFAULT_TIMEOUT ).build ()),
5187 DEFAULT_MAX_ATTEMPTS , Duration .ofMillis ((long ) DEFAULT_TIMEOUT * DEFAULT_MAX_ATTEMPTS ));
5288 }
5389
5490 public RedisClusterClient (Set <HostAndPort > nodes , String user , String password ) {
55- super (new ClusterConnectionProvider (nodes , DefaultJedisClientConfig .builder ().user (user ).password (password ).build ()),
56- DEFAULT_MAX_ATTEMPTS , Duration .ofMillis ((long ) Protocol .DEFAULT_TIMEOUT * DEFAULT_MAX_ATTEMPTS ));
91+ super (
92+ new ClusterConnectionProvider (nodes ,
93+ DefaultJedisClientConfig .builder ().user (user ).password (password ).build ()),
94+ DEFAULT_MAX_ATTEMPTS ,
95+ Duration .ofMillis ((long ) Protocol .DEFAULT_TIMEOUT * DEFAULT_MAX_ATTEMPTS ));
5796 }
5897
59- private RedisClusterClient (CommandExecutor commandExecutor , ConnectionProvider connectionProvider , CommandObjects commandObjects , RedisProtocol redisProtocol , Cache cache ) {
98+ private RedisClusterClient (CommandExecutor commandExecutor , ConnectionProvider connectionProvider ,
99+ CommandObjects commandObjects , RedisProtocol redisProtocol , Cache cache ) {
60100 super (commandExecutor , connectionProvider , commandObjects , redisProtocol , cache );
61101 }
62102
@@ -70,8 +110,8 @@ static public class Builder extends ClusterClientBuilder<RedisClusterClient> {
70110
71111 @ Override
72112 protected RedisClusterClient createClient () {
73- return new RedisClusterClient (commandExecutor , connectionProvider , commandObjects , clientConfig . getRedisProtocol (),
74- cache );
113+ return new RedisClusterClient (commandExecutor , connectionProvider , commandObjects ,
114+ clientConfig . getRedisProtocol (), cache );
75115 }
76116 }
77117
@@ -95,7 +135,8 @@ public Map<String, ConnectionPool> getClusterNodes() {
95135 /**
96136 * Returns the connection for one of the 16,384 slots.
97137 * @param slot the slot to retrieve the connection for.
98- * @return connection of the provided slot. {@code close()} of this connection must be called after use.
138+ * @return connection of the provided slot. {@code close()} of this connection must be called
139+ * after use.
99140 */
100141 public Connection getConnectionFromSlot (int slot ) {
101142 return ((ClusterConnectionProvider ) provider ).getConnectionFromSlot (slot );
@@ -125,7 +166,8 @@ public void ssubscribe(BinaryJedisShardedPubSub jedisPubSub, final byte[]... cha
125166
126167 @ Override
127168 public ClusterPipeline pipelined () {
128- return new ClusterPipeline ((ClusterConnectionProvider ) provider , (ClusterCommandObjects ) commandObjects );
169+ return new ClusterPipeline ((ClusterConnectionProvider ) provider ,
170+ (ClusterCommandObjects ) commandObjects );
129171 }
130172
131173 /**
@@ -140,9 +182,9 @@ public AbstractTransaction transaction(boolean doMulti) {
140182
141183 public final <T > T executeCommandToReplica (CommandObject <T > commandObject ) {
142184 if (!(executor instanceof ClusterCommandExecutor )) {
143- throw new UnsupportedOperationException ("Support only execute to replica in ClusterCommandExecutor" );
185+ throw new UnsupportedOperationException (
186+ "Support only execute to replica in ClusterCommandExecutor" );
144187 }
145188 return ((ClusterCommandExecutor ) executor ).executeCommandToReplica (commandObject );
146189 }
147190}
148-
0 commit comments