<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-redis-client</artifactId> <version>4.2.2</version> </dependency> import io.vertx.redis.client.Redis; import io.vertx.redis.client.RedisOptions; import io.vertx.core.Vertx; public class RedisClientExample { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); RedisOptions options = new RedisOptions() .setConnectionString("redis://localhost:6379") .setPassword("your_password"); Redis client = Redis.createClient(vertx, options); client.close(); } } import io.vertx.redis.client.Response; import io.vertx.core.buffer.Buffer; public class RedisOperationsExample { public static void main(String[] args) { client.set(Buffer.buffer("key"), Buffer.buffer("value"), res -> { if (res.succeeded()) { System.out.println("Value set successfully"); } else { System.out.println("Failed to set value"); } }); client.get(Buffer.buffer("key"), res -> { if (res.succeeded()) { Response response = res.result(); System.out.println("Value: " + response.toString()); } else { System.out.println("Failed to get value"); } }); client.multi(res -> { if (res.succeeded()) { System.out.println("Transaction started"); } else { System.out.println("Failed to start transaction"); } }); client.exec(res -> { if (res.succeeded()) { System.out.println("Transaction executed successfully"); } else { System.out.println("Failed to execute transaction"); } }); client.subscribe(Buffer.buffer("channel"), res -> { if (res.succeeded()) { System.out.println("Subscribed to channel"); } else { System.out.println("Failed to subscribe"); } }); } }


上一篇:
下一篇:
切换中文