Skip to content

Commit

Permalink
Add Jedis clients
Browse files Browse the repository at this point in the history
Signed-off-by: acarbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Oct 10, 2023
1 parent 4117580 commit 2799ac1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package javababushka.benchmarks.clients;

import javababushka.benchmarks.utils.ConnectionSettings;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

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

public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_PORT = 6379;

protected Jedis jedisResource;

@Override
public void connectToRedis() {
JedisPool pool = new JedisPool(DEFAULT_HOST, DEFAULT_PORT);
jedisResource = pool.getResource();
}

@Override
public void closeConnection() {
try {
jedisResource.close();
} catch (Exception ignored) {
}
}

@Override
public String getName() {
return "Jedis";
}

@Override
public void connectToRedis(ConnectionSettings connectionSettings) {
jedisResource =
new Jedis(connectionSettings.host, connectionSettings.port, connectionSettings.useSsl);
jedisResource.connect();
}

public String info() {
return jedisResource.info();
}

public String info(String section) {
return jedisResource.info(section);
}

@Override
public void set(String key, String value) {
jedisResource.set(key, value);
}

@Override
public String get(String key) {
return jedisResource.get(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package javababushka.benchmarks.clients;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
* A jedis client with pseudo-sync capabilities. Jedis doesn't provide async API
* https://github.com/redis/jedis/issues/241
*
* <p>See: https://github.com/redis/jedis
*/
public class JedisPseudoAsyncClient extends JedisClient implements AsyncClient {
@Override
public Future<?> asyncSet(String key, String value) {
return CompletableFuture.runAsync(() -> super.set(key, value));
}

@Override
public Future<String> asyncGet(String key) {
return CompletableFuture.supplyAsync(() -> super.get(key));
}

@Override
public <T> T waitForResult(Future<T> future) {
return waitForResult(future, DEFAULT_TIMEOUT);
}

@Override
public <T> T waitForResult(Future<T> future, long timeout) {
try {
return future.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception ignored) {
return null;
}
}

@Override
public String getName() {
return "Jedis pseudo-async";
}
}

0 comments on commit 2799ac1

Please sign in to comment.