Skip to content

Commit 89d2ad9

Browse files
Merge pull request #2559 from uglide/im/fix-jedis-code-examples
Fix Jedis code examples after the final changes to the RedisClient class
2 parents 9f73b3b + 6fa2842 commit 89d2ad9

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

content/develop/clients/jedis/amr.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ JedisClientConfig config = DefaultJedisClientConfig.builder()
159159
.ssl(true).sslSocketFactory(sslFactory)
160160
.build();
161161

162-
RedisClient jedis = new RedisClient(
163-
new HostAndPort("<host>", <port>),
164-
config
165-
);
162+
RedisClient jedis = RedisClient.builder()
163+
.hostAndPort(new HostAndPort("<host>", <port>))
164+
.clientConfig(config)
165+
.build();
166166

167167
// Test the connection.
168168
System.out.println(String.format("Database size is %d", jedis.dbSize()));

content/develop/clients/jedis/connect.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import redis.clients.jedis.RedisClient;
3636

3737
public class Main {
3838
public static void main(String[] args) {
39-
RedisClient jedis = new RedisClient("redis://localhost:6379");
39+
RedisClient jedis = RedisClient.create("redis://localhost:6379");
4040

4141
// Code that interacts with Redis...
4242

@@ -73,7 +73,7 @@ import redis.clients.jedis.HostAndPort;
7373
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
7474
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
7575
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7380));
76-
RedisClusterClient jedis = new RedisClusterClient(jedisClusterNodes);
76+
RedisClusterClient jedis = RedisClusterClient.create(jedisClusterNodes);
7777
```
7878

7979
### Connect to your production Redis with TLS
@@ -129,7 +129,10 @@ public class Main {
129129
.password("secret!") // use your Redis password
130130
.build();
131131

132-
RedisClient jedis = new RedisClient(address, config);
132+
RedisClient jedis = RedisClient.builder()
133+
.hostAndPort(address)
134+
.clientConfig(config)
135+
.build();
133136
jedis.set("foo", "bar");
134137
System.out.println(jedis.get("foo")); // prints bar
135138
}
@@ -195,7 +198,11 @@ DefaultJedisClientConfig config = DefaultJedisClientConfig
195198

196199
CacheConfig cacheConfig = CacheConfig.builder().maxSize(1000).build();
197200

198-
RedisClient client = new RedisClient(endpoint, config, cacheConfig);
201+
RedisClient client = RedisClient.builder()
202+
.hostAndPort(endpoint)
203+
.clientConfig(config)
204+
.cacheConfig(cacheConfig)
205+
.build();
199206
```
200207

201208
Once you have connected, the usual Redis commands will work transparently
@@ -308,7 +315,7 @@ Here is a simplified connection lifecycle in a pool:
308315
7. The connection becomes evictable if the number of connections is greater than `minIdle`.
309316
8. The connection is ready to be closed.
310317

311-
Pass a `ConnectionPoolConfig` object to the `RedisClient` constructor to configure the pool.
318+
Pass a `ConnectionPoolConfig` object to the `RedisClient` builder to configure the pool.
312319
(`ConnectionPoolConfig` is a wrapper around `GenericObjectPoolConfig` from [Apache Commons Pool2](https://commons.apache.org/proper/commons-pool/apidocs/org/apache/commons/pool2/impl/GenericObjectPoolConfig.html)).
313320

314321
```java
@@ -333,7 +340,10 @@ poolConfig.setTestWhileIdle(true);
333340
// controls the period between checks for idle connections in the pool
334341
poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(1));
335342

336-
RedisClient jedis = new RedisClient(poolConfig, "localhost", 6379);
343+
RedisClient jedis = RedisClient.builder()
344+
.hostAndPort("localhost", 6379)
345+
.poolConfig(poolConfig)
346+
.build();
337347
```
338348

339349
### Retrying a command after a connection failure
@@ -347,11 +357,11 @@ shows a retry loop that uses a simple
347357
strategy:
348358

349359
```java
350-
RedisClient jedis = new RedisClient(
351-
new HostAndPort("localhost", 6379),
352-
clientConfig,
353-
poolConfig
354-
);
360+
RedisClient jedis = RedisClient.builder()
361+
.hostAndPort("localhost", 6379)
362+
.poolConfig(poolConfig)
363+
.clientConfig(config)
364+
.build();
355365

356366
// Set max retry attempts
357367
final int MAX_RETRIES = 5;

content/develop/clients/jedis/error-handling.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Catch specific exceptions that represent unrecoverable errors and re-throw them
6767
for a full description):
6868

6969
```java
70-
try (RedisClient jedis = new RedisClient()) {
70+
try (RedisClient jedis = RedisClient.create()) {
7171
String result = jedis.get(key);
7272
} catch (JedisDataException e) {
7373
// This indicates a bug in our code
@@ -82,7 +82,7 @@ Catch specific errors and fall back to an alternative, where possible (see
8282
for a full description):
8383

8484
```java
85-
try (RedisClient jedis = new RedisClient()) {
85+
try (RedisClient jedis = RedisClient.create()) {
8686
String cachedValue = jedis.get(key);
8787
if (cachedValue != null) {
8888
return cachedValue;
@@ -107,7 +107,7 @@ int maxRetries = 3;
107107
int retryDelay = 100;
108108

109109
for (int attempt = 0; attempt < maxRetries; attempt++) {
110-
try (RedisClient jedis = new RedisClient()) {
110+
try (RedisClient jedis = RedisClient.create()) {
111111
return jedis.get(key);
112112
} catch (JedisConnectionException e) {
113113
if (attempt < maxRetries - 1) {
@@ -132,7 +132,7 @@ Log non-critical errors and continue (see
132132
for a full description):
133133

134134
```java
135-
try (RedisClient jedis = new RedisClient()) {
135+
try (RedisClient jedis = RedisClient.create()) {
136136
jedis.setex(key, 3600, value);
137137
} catch (JedisConnectionException e) {
138138
logger.warn("Failed to cache " + key + ", continuing without cache");

content/develop/clients/jedis/produsage.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,19 @@ connection or issuing a command, it can end up hanging indefinitely.
7676
You can prevent this from happening by setting timeouts for socket
7777
reads and writes and for opening connections.
7878

79-
To set a timeout for a connection, use the `RedisClient` constructor with the `timeout` parameter, or use `JedisClientConfig` with the `socketTimeout` and `connectionTimeout` parameters.
79+
To set a timeout for a connection, use `JedisClientConfig` with the `socketTimeout` and `connectionTimeout` parameters.
8080
(The socket timeout is the maximum time allowed for reading or writing data while executing a
8181
command. The connection timeout is the maximum time allowed for establishing a new connection.)
8282

8383
```java
84-
HostAndPort hostAndPort = new HostAndPort("localhost", 6379);
85-
86-
RedisClient jedisWithTimeout = new RedisClient(hostAndPort,
87-
DefaultJedisClientConfig.builder()
88-
.socketTimeoutMillis(5000) // set timeout to 5 seconds
89-
.connectionTimeoutMillis(5000) // set connection timeout to 5 seconds
90-
.build(),
91-
poolConfig
92-
);
84+
RedisClient jedisWithTimeout = RedisClient.builder()
85+
.hostAndPort("localhost", 6379)
86+
.clientConfig(
87+
DefaultJedisClientConfig.builder()
88+
.socketTimeoutMillis(5000) // set timeout to 5 seconds
89+
.connectionTimeoutMillis(5000) // set connection timeout to 5 seconds
90+
.build())
91+
.build();
9392
```
9493

9594
### Health checks
@@ -100,11 +99,21 @@ every few seconds). You can do this using a simple
10099
[`PING`]({{< relref "/commands/ping" >}}) command:
101100

102101
```java
103-
try (RedisClient jedis = new RedisClient()) {
104-
if (! "PONG".equals(jedis.ping())) {
105-
// Report problem.
106-
}
107-
}
102+
ConnectionPoolConfig poolConfig = new ConnectionPoolConfig();
103+
poolConfig.setMaxTotal(8);
104+
poolConfig.setMaxIdle(8);
105+
poolConfig.setMinIdle(0);
106+
poolConfig.setBlockWhenExhausted(true);
107+
poolConfig.setMaxWait(Duration.ofSeconds(1));
108+
109+
// Enables sending a PING command once every second while the connection is idle.
110+
poolConfig.setTestWhileIdle(true);
111+
poolConfig.setTimeBetweenEvictionRuns(Duration.ofSeconds(1));
112+
113+
RedisClient jedis = RedisClient.builder()
114+
.hostAndPort("localhost", 6379)
115+
.poolConfig(poolConfig)
116+
.build();
108117
```
109118

110119
Health checks help to detect problems as soon as possible without

0 commit comments

Comments
 (0)