Skip to content

Commit b9d08c0

Browse files
uglideggivo
andauthored
Introduce new client classes (#4355)
* Relax type checking for ClusterClientBuilder * Add new classes * Use INIT_NO_ERROR_PROPERTY constant from new class * Deprecate old classes * Deprecate public constructors in UnifiedJedis * Add migration tests for JedisCluster constructors used by SDR, Conductor and other OSS projects * Fix formatting and imports * Replace usages of JedisPooled with RedisClient * Rename "pooled" tests * Deprecate JedisPool and JedisSentinelPool Provide clear guidance which client class should be used instead. * Replace JedisCluster with RedisClusterClient in tests * Fix javadocs and enable formatter exclusion tags * Fix modifiers order * Fix cluster tests * Update docs * Use RedisClient in doctests * resolve conflict after rebase * Update src/main/java/redis/clients/jedis/RedisClient.java Co-authored-by: Ivo Gaydazhiev <ivo.gaydazhiev@redis.com> * Use RedisClient in PingStrategy * Reformat * Use lettuce-style factory methods * Update examples --------- Co-authored-by: ggivo <ivo.gaydazhiev@redis.com>
1 parent bce2ccc commit b9d08c0

File tree

117 files changed

+2185
-762
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+2185
-762
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ Next, you'll need to connect to Redis. Consider installing a redis server with d
6666
docker run -p 6379:6379 -it redis:latest
6767
```
6868

69-
For many applications, it's best to use a connection pool. You can instantiate a JedisPooled like so:
69+
You can instantiate a RedisClient like so:
7070

7171
```java
72-
JedisPooled jedis = new JedisPooled("localhost", 6379);
72+
RedisClient jedis = RedisClient.builder().hostAndPort("localhost", 6379).build();
7373
```
7474

7575
Now you can send commands:
@@ -81,16 +81,16 @@ jedis.sadd("planets", "Venus");
8181
## Connecting to a Redis cluster
8282

8383
Jedis lets you connect to Redis Clusters, supporting the [Redis Cluster Specification](https://redis.io/topics/cluster-spec).
84-
To do this, you'll need to connect using `JedisCluster`. See the example below:
84+
To do this, you'll need to connect using `RedisClusterClient`. See the example below:
8585

8686
```java
8787
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
8888
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
8989
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
90-
JedisCluster jedis = new JedisCluster(jedisClusterNodes);
90+
RedisClusterClient jedis = RedisClusterClient.builder().nodes(jedisClusterNodes).build();
9191
```
9292

93-
Now you can use the `JedisCluster` instance and send commands like you would with a standard pooled connection:
93+
Now you can use the `RedisClusterClient` instance and send commands like you would with a standard pooled connection:
9494

9595
```java
9696
jedis.sadd("planets", "Mars");

docs/advanced-usage.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,16 @@ public class DockerNATMapper implements HostAndPortMapper {
255255
}
256256
```
257257

258-
Then, instantiate this class and pass it to the JedisCluster constructor:
258+
Then, instantiate this class and pass it to the RedisClusterClient builder:
259259

260260
```java
261261
Map<HostAndPort, HostAndPort> nodeMapping = new HashMap<>();
262-
nodeMapping.put(new HostAndPort("172.18.0.2", 6379), new HostAndPort("my-redis.example.com", 7001));
262+
nodeMapping.put(new HostAndPort("172.18.0.2", 6379), new HostAndPort("my-redis.example.com", 7001));
263263
nodeMapping.put(new HostAndPort("172.18.0.3", 6379), new HostAndPort("my-redis.example.com", 7002));
264264
nodeMapping.put(new HostAndPort("172.18.0.4", 6379), new HostAndPort("my-redis.example.com", 7002));
265265

266266
Set<HostAndPort> initialNodes = new HashSet<>();
267-
// seed node
267+
// seed node
268268
initialNodes.add(new HostAndPort("my-redis.example.com", 7001));
269269

270270
HostAndPortMapper mapper = new DockerNATMapper(nodeMapping);
@@ -275,10 +275,13 @@ JedisClientConfig jedisClientConfig = DefaultJedisClientConfig.builder()
275275
.hostAndPortMapper(mapper)
276276
.build();
277277

278-
JedisCluster jedisCluster = new JedisCluster(initialNodes, jedisClientConfig);
278+
RedisClusterClient jedisCluster = RedisClusterClient.builder()
279+
.nodes(initialNodes)
280+
.clientConfig(jedisClientConfig)
281+
.build();
279282
```
280283

281-
Now, when JedisCluster discovers a node at "172.18.0.2:6379", the mapper will translate it to "localhost:7001" before attempting to connect.
284+
Now, when RedisClusterClient discovers a node at "172.18.0.2:6379", the mapper will translate it to "localhost:7001" before attempting to connect.
282285

283286
### Implementing with a Lambda Expression
284287
Since HostAndPortMapper is a functional interface (it has only one abstract method), you can also provide the implementation more concisely using a lambda expression. This is often preferred for simpler, inline mapping logic.
@@ -300,7 +303,10 @@ JedisClientConfig jedisClientConfig = DefaultJedisClientConfig.builder()
300303
.hostAndPortMapper(mapper)
301304
.build();
302305

303-
JedisCluster jedisCluster = new JedisCluster(initialNodes, jedisClientConfig);
306+
RedisClusterClient jedisCluster = RedisClusterClient.builder()
307+
.nodes(initialNodes)
308+
.clientConfig(jedisClientConfig)
309+
.build();
304310
```
305311

306312
## Miscellaneous

docs/redisearch.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ To use RediSearch features with Jedis, you'll need to use an implementation of R
44

55
## Creating the RediSearch client
66

7-
Initializing the client with JedisPooled:
7+
Initializing the client with RedisClient:
88

99
```java
10-
JedisPooled client = new JedisPooled("localhost", 6379);
10+
RedisClient client = RedisClient.builder().hostAndPort("localhost", 6379).build();
1111
```
1212

13-
Initializing the client with JedisCluster:
13+
Initializing the client with RedisClusterClient:
1414

1515
```java
1616
Set<HostAndPort> nodes = new HashSet<>();
1717
nodes.add(new HostAndPort("127.0.0.1", 7379));
1818
nodes.add(new HostAndPort("127.0.0.1", 7380));
1919

20-
JedisCluster client = new JedisCluster(nodes);
20+
RedisClusterClient client = RedisClusterClient.builder().nodes(nodes).build();
2121
```
2222

2323
## Indexing and querying

docs/redisjson.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ Let's see how this works.
99

1010
## Creating with RedisJSON client
1111

12-
First, let's create a `JedisPooled` client instance:
12+
First, let's create a `RedisClient` client instance:
1313

1414
```java
15-
JedisPooled client = new JedisPooled("localhost", 6479);
15+
RedisClient client = RedisClient.builder().hostAndPort("localhost", 6479).build();
1616
```
1717

18-
Or, a `JedisCluster` client instance:
18+
Or, a `RedisClusterClient` client instance:
1919

2020
```java
2121
Set<HostAndPort> nodes = new HashSet<>();
2222
nodes.add(new HostAndPort("127.0.0.1", 7379));
2323
nodes.add(new HostAndPort("127.0.0.1", 7380));
2424

25-
JedisCluster client = new JedisCluster(nodes);
25+
RedisClusterClient client = RedisClusterClient.builder().nodes(nodes).build();
2626
```
2727

2828
Now we can start working with JSON. For these examples, we'll be using [GSON](https://github.com/google/gson)

hbase-formatter.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation" value="do not insert"/>
1717
<setting id="org.eclipse.jdt.core.formatter.blank_lines_before_field" value="0"/>
1818
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while" value="do not insert"/>
19-
<setting id="org.eclipse.jdt.core.formatter.use_on_off_tags" value="false"/>
19+
<setting id="org.eclipse.jdt.core.formatter.use_on_off_tags" value="true"/>
2020
<setting id="org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_annotation_type_member_declaration" value="do not insert"/>
2121
<setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement" value="do not insert"/>
2222
<setting id="org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator" value="do not insert"/>

src/main/java/redis/clients/jedis/Jedis.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,62 @@
3333
import redis.clients.jedis.util.KeyValue;
3434
import redis.clients.jedis.util.Pool;
3535

36+
/**
37+
* Jedis is a lightweight Redis client that uses a single, non-pooled connection to Redis.
38+
* <p>
39+
* <b>Important:</b> For most production use cases, {@link RedisClient} is the recommended and
40+
* preferred option. {@code RedisClient} provides connection pooling, better resource management,
41+
* and improved performance for typical applications.
42+
* </p>
43+
* <p>
44+
* <b>When to use Jedis:</b>
45+
* </p>
46+
* <ul>
47+
* <li><b>Short-lived scripts or utilities:</b> When you need a simple, lightweight client for
48+
* one-off operations or command-line tools.</li>
49+
* <li><b>Testing and development:</b> For unit tests or local development where connection pooling
50+
* overhead is unnecessary.</li>
51+
* <li><b>Fine-grained connection control:</b> Advanced scenarios requiring explicit control over
52+
* individual connections, such as managing connection lifecycle manually or implementing custom
53+
* connection strategies.</li>
54+
* <li><b>Single-threaded applications:</b> Applications that execute Redis commands sequentially
55+
* from a single thread and don't benefit from connection pooling.</li>
56+
* </ul>
57+
* <p>
58+
* <b>When to use RedisClient instead:</b>
59+
* </p>
60+
* <ul>
61+
* <li><b>Production applications:</b> Any multi-threaded or high-throughput application should use
62+
* {@link RedisClient} for its connection pooling capabilities.</li>
63+
* <li><b>Web applications:</b> Server applications handling concurrent requests benefit from
64+
* connection pooling to avoid connection overhead.</li>
65+
* <li><b>Long-running services:</b> Applications that maintain persistent connections to Redis
66+
* should use {@link RedisClient} for better resource management.</li>
67+
* <li><b>Default choice:</b> If you're unsure which to use, choose {@link RedisClient}.</li>
68+
* </ul>
69+
* <p>
70+
* <b>Usage example:</b>
71+
* </p>
72+
*
73+
* <pre>
74+
* {
75+
* &#64;code
76+
* // Simple usage for a short-lived operation
77+
* try (Jedis jedis = new Jedis("localhost", 6379)) {
78+
* jedis.set("key", "value");
79+
* String value = jedis.get("key");
80+
* }
81+
* }
82+
* </pre>
83+
* <p>
84+
* <b>Note:</b> Each {@code Jedis} instance maintains a single connection. For concurrent access
85+
* from multiple threads, either use {@link RedisClient} with connection pooling, or create
86+
* separate {@code Jedis} instances per thread (not recommended for production).
87+
* </p>
88+
*
89+
* @see RedisClient for the recommended pooled client for production use
90+
* @see JedisPool for legacy pooled connections (deprecated, use RedisClient instead)
91+
*/
3692
public class Jedis implements ServerCommands, DatabaseCommands, JedisCommands, JedisBinaryCommands,
3793
ControlCommands, ControlBinaryCommands, ClusterCommands, ModuleCommands, GenericControlCommands,
3894
SentinelCommands, CommandCommands, Closeable {

src/main/java/redis/clients/jedis/JedisCluster.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
import redis.clients.jedis.providers.ConnectionProvider;
1919
import redis.clients.jedis.util.JedisClusterCRC16;
2020

21+
/**
22+
* JedisCluster is a client for Redis Cluster deployments.
23+
*
24+
* @deprecated Use {@link RedisClusterClient} instead. RedisClusterClient provides the same functionality
25+
* with a cleaner API and simplified constructor options. For basic usage, simple
26+
* constructors are available. For advanced configuration, use {@link RedisClusterClient#builder()}.
27+
*/
28+
@Deprecated
2129
public class JedisCluster extends UnifiedJedis {
2230

2331
public static final String INIT_NO_ERROR_PROPERTY = "jedis.cluster.initNoError";

src/main/java/redis/clients/jedis/JedisClusterInfoCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import redis.clients.jedis.exceptions.JedisException;
3232
import redis.clients.jedis.util.SafeEncoder;
3333

34-
import static redis.clients.jedis.JedisCluster.INIT_NO_ERROR_PROPERTY;
34+
import static redis.clients.jedis.RedisClusterClient.INIT_NO_ERROR_PROPERTY;
3535

3636
@Internal
3737
public class JedisClusterInfoCache {

src/main/java/redis/clients/jedis/JedisFactory.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@
1717
import redis.clients.jedis.util.JedisURIHelper;
1818

1919
/**
20-
* PoolableObjectFactory custom impl.
20+
* PooledObjectFactory implementation for creating and managing {@link Jedis} instances in connection pools.
21+
* <p>
22+
* This factory is used internally by {@link JedisPool} and {@link JedisSentinelPool} to create, validate,
23+
* and destroy pooled Jedis connections.
24+
* </p>
25+
*
26+
* @deprecated JedisFactory is used exclusively with the deprecated {@link JedisPool} and {@link JedisSentinelPool}
27+
* classes. For modern Redis clients ({@link RedisClient}, {@link RedisSentinelClient}), the framework
28+
* uses {@link ConnectionFactory} internally, which manages {@link Connection} objects instead of
29+
* {@link Jedis} instances. There is no direct replacement for JedisFactory as connection management
30+
* is handled automatically by the new client architecture.
2131
*/
22-
// Legacy
32+
@Deprecated
2333
public class JedisFactory implements PooledObjectFactory<Jedis> {
2434

2535
private static final Logger logger = LoggerFactory.getLogger(JedisFactory.class);

src/main/java/redis/clients/jedis/JedisPool.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
import redis.clients.jedis.util.JedisURIHelper;
1616
import redis.clients.jedis.util.Pool;
1717

18-
// Legacy
18+
/**
19+
* JedisPool is a pooled connection client for standalone Redis servers.
20+
*
21+
* @deprecated Use {@link RedisClient} instead. RedisClient provides the same functionality
22+
* with a cleaner API and simplified constructor options. For basic usage, simple
23+
* constructors are available. For advanced configuration, use {@link RedisClient#builder()}.
24+
*/
25+
@Deprecated
1926
public class JedisPool extends Pool<Jedis> {
2027

2128
private static final Logger log = LoggerFactory.getLogger(JedisPool.class);

0 commit comments

Comments
 (0)