package examples;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.net.SocketAddress;
import io.vertx.redis.client.*;
public class RedisExamples {
public void example1(Vertx vertx) {
Redis.createClient(vertx, new RedisOptions())
.connect(onConnect -> {
if (onConnect.succeeded()) {
Redis client = onConnect.result();
}
});
}
public void example2(Vertx vertx) {
RedisOptions options = new RedisOptions()
.setPassword("abracadabra")
.setSelect(1);
Redis.createClient(vertx, options)
.connect(onConnect -> {
if (onConnect.succeeded()) {
Redis client = onConnect.result();
}
});
}
public void example3(Redis client) {
RedisAPI redis = RedisAPI.api(client);
redis.get("mykey", res -> {
if (res.succeeded()) {
}
});
}
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)
.addEndpoint(SocketAddress.inetSocketAddress(5000, "127.0.0.1"))
.addEndpoint(SocketAddress.inetSocketAddress(5001, "127.0.0.1"))
.addEndpoint(SocketAddress.inetSocketAddress(5002, "127.0.0.1"))
.setMasterName("sentinel7000")
.setRole(RedisRole.MASTER))
.connect(onConnect -> {
onConnect.result()
.send(Request.cmd(Command.INFO), info -> {
});
});
}
public void example6() {
final RedisOptions options = new RedisOptions()
.addEndpoint(SocketAddress.inetSocketAddress(7000, "127.0.0.1"))
.addEndpoint(SocketAddress.inetSocketAddress(7001, "127.0.0.1"))
.addEndpoint(SocketAddress.inetSocketAddress(7002, "127.0.0.1"))
.addEndpoint(SocketAddress.inetSocketAddress(7003, "127.0.0.1"))
.addEndpoint(SocketAddress.inetSocketAddress(7004, "127.0.0.1"))
.addEndpoint(SocketAddress.inetSocketAddress(7005, "127.0.0.1"));
}
public void example7(Vertx vertx) {
Redis.createClient(vertx, new RedisOptions())
.connect(onConnect -> {
if (onConnect.succeeded()) {
Redis client = onConnect.result();
client.handler(message -> {
});
}
});
}
public void example8(Redis redis) {
redis.send(Request.cmd(Command.PUBLISH).arg("channel1").arg("Hello World!"), res -> {
if (res.succeeded()) {
}
});
}
public void example9(Vertx vertx) {
Redis.createClient(vertx, SocketAddress.domainSocketAddress("/tmp/redis.sock"))
.connect(onConnect -> {
if (onConnect.succeeded()) {
Redis client = onConnect.result();
}
});
}
public void example10() {
class RedisVerticle extends AbstractVerticle {
private static final int MAX_RECONNECT_RETRIES = 16;
private RedisOptions options = new RedisOptions();
private Redis client;
@Override
public void start() {
createRedisClient(onCreate -> {
if (onCreate.succeeded()) {
}
});
}
private void createRedisClient(Handler<AsyncResult<Redis>> handler) {
Redis.createClient(vertx, options)
.connect(onConnect -> {
if (onConnect.succeeded()) {
client = onConnect.result();
client.exceptionHandler(e -> {
attemptReconnect(0);
});
}
handler.handle(onConnect);
});
}
private void attemptReconnect(int retry) {
if (retry > MAX_RECONNECT_RETRIES) {
} else {
long backoff = (long) (Math.pow(2, MAX_RECONNECT_RETRIES - Math.max(MAX_RECONNECT_RETRIES - retry, 9)) * 10);
vertx.setTimer(backoff, timer -> createRedisClient(onReconnect -> {
if (onReconnect.failed()) {
attemptReconnect(retry + 1);
}
}));
}
}
}
}
}