package examples;
import io.vertx.core.*;
import io.vertx.redis.client.*;
public class RedisExamples {
public void example1(Vertx vertx) {
Redis.createClient(vertx)
.connect()
.onSuccess(conn -> {
});
}
public void example2(Vertx vertx) {
Redis.createClient(
vertx,
"redis://:abracadabra@localhost:6379/1")
.connect()
.onSuccess(conn -> {
});
}
public void example3(RedisConnection client) {
RedisAPI redis = RedisAPI.api(client);
redis
.get("mykey")
.onSuccess(value -> {
});
}
public void example4(Response response) {
if (response.type() == ResponseType.MULTI) {
for (Response item : response) {
}
}
}
public void example5(Vertx vertx) {
Redis.createClient(
vertx,
new RedisOptions()
.setType(RedisClientType.SENTINEL)
.addConnectionString("redis://127.0.0.1:5000")
.addConnectionString("redis://127.0.0.1:5001")
.addConnectionString("redis://127.0.0.1:5002")
.setMasterName("sentinel7000")
.setRole(RedisRole.MASTER))
.connect()
.onSuccess(conn -> {
conn.send(Request.cmd(Command.INFO))
.onSuccess(info -> {
});
});
}
public void example6() {
final RedisOptions options = new RedisOptions()
.addConnectionString("redis://127.0.0.1:7000")
.addConnectionString("redis://127.0.0.1:7001")
.addConnectionString("redis://127.0.0.1:7002")
.addConnectionString("redis://127.0.0.1:7003")
.addConnectionString("redis://127.0.0.1:7004")
.addConnectionString("redis://127.0.0.1:7005");
}
public void example7(Vertx vertx) {
Redis.createClient(vertx, new RedisOptions())
.connect()
.onSuccess(conn -> {
conn.handler(message -> {
});
});
}
public void example8(RedisConnection redis) {
redis.send(Request.cmd(Command.PUBLISH).arg("channel1").arg("Hello World!"))
.onSuccess(res -> {
});
}
public void example9(Vertx vertx) {
Redis.createClient(vertx, "unix:///tmp/redis.sock")
.connect()
.onSuccess(conn -> {
});
}
public void example10() {
class RedisVerticle extends AbstractVerticle {
private static final int MAX_RECONNECT_RETRIES = 16;
private final RedisOptions options = new RedisOptions();
private RedisConnection client;
@Override
public void start() {
createRedisClient()
.onSuccess(conn -> {
});
}
private Future<RedisConnection> createRedisClient() {
Promise<RedisConnection> promise = Promise.promise();
Redis.createClient(vertx, options)
.connect()
.onSuccess(conn -> {
conn.exceptionHandler(e -> {
attemptReconnect(0);
});
promise.complete(conn);
});
return promise.future();
}
private void attemptReconnect(int retry) {
if (retry > MAX_RECONNECT_RETRIES) {
} else {
long backoff = (long) (Math.pow(2, Math.min(retry, 10)) * 10);
vertx.setTimer(backoff, timer -> {
createRedisClient()
.onFailure(t -> attemptReconnect(retry + 1));
});
}
}
}
}
public void example11(Vertx vertx) {
Redis.createClient(vertx, "redis://localhost:7006")
.send(Request.cmd(Command.PING))
.onSuccess(res -> {
});
}
public void example12(Vertx vertx) {
Redis.createClient(
vertx,
new RedisOptions()
.setConnectionString("redis://localhost:7006")
.setMaxPoolSize(8)
.setMaxWaitingHandlers(32))
.send(Request.cmd(Command.PING))
.onSuccess(res -> {
});
}
}