Skip to content

Commit

Permalink
Update jedis sync client with thread pooling (valkey-io#978)
Browse files Browse the repository at this point in the history
* Update jedis sync client with thread pooling (valkey-io#86)

* Add jedis threadpool for standalone client

Signed-off-by: Andrew Carbonetto <[email protected]>

---------

Signed-off-by: Andrew Carbonetto <[email protected]>

* Java-Jedis: Only set the pool for standalone

Signed-off-by: Andrew Carbonetto <[email protected]>

* Spotless

Signed-off-by: Andrew Carbonetto <[email protected]>

* Rename field in jedis client

Signed-off-by: Andrew Carbonetto <[email protected]>

---------

Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto authored Feb 26, 2024
1 parent 38626f4 commit 3fb2ebd
Showing 1 changed file with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
import java.util.Set;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.commands.JedisCommands;

/** A Jedis client with sync capabilities. See: https://github.com/redis/jedis */
public class JedisClient implements SyncClient {

private JedisCommands jedis;
boolean isClusterMode;
private JedisPool jedisStandalonePool;
private JedisCluster jedisCluster;

@Override
public void closeConnection() {
// nothing to do
if (jedisCluster != null) {
jedisCluster.close();
}
if (jedisStandalonePool != null) {
jedisStandalonePool.close();
}
}

@Override
Expand All @@ -27,27 +33,38 @@ public String getName() {

@Override
public void connectToRedis(ConnectionSettings connectionSettings) {
if (connectionSettings.clusterMode) {
jedis =
isClusterMode = connectionSettings.clusterMode;
if (isClusterMode) {
jedisCluster =
new JedisCluster(
Set.of(new HostAndPort(connectionSettings.host, connectionSettings.port)),
DefaultJedisClientConfig.builder().ssl(connectionSettings.useSsl).build());
} else {
try (JedisPool pool =
jedisStandalonePool =
new JedisPool(
connectionSettings.host, connectionSettings.port, connectionSettings.useSsl)) {
jedis = pool.getResource();
}
connectionSettings.host, connectionSettings.port, connectionSettings.useSsl);
}
}

@Override
public void set(String key, String value) {
jedis.set(key, value);
if (isClusterMode) {
jedisCluster.set(key, value);
} else {
try (Jedis jedis = jedisStandalonePool.getResource()) {
jedis.set(key, value);
}
}
}

@Override
public String get(String key) {
return jedis.get(key);
if (isClusterMode) {
return jedisCluster.get(key);
} else {
try (Jedis jedis = jedisStandalonePool.getResource()) {
return jedis.get(key);
}
}
}
}

0 comments on commit 3fb2ebd

Please sign in to comment.