1. How to use custom Redis Commands
As the list of commands which are supported out-of-box by both RedisClient and ReactiveRedisClient depends on what is available in vertx-redis-client, then there might be case when you need a command which is not (yet) available via vertx-redis-client.
In such case (if you don’t want to wait for the new command to be supported in vertx-redis-client), you can implement it in either RedisClient or ReactiveRedisClient.
In order to do so, you will need:
-
Generate a new
Commandbased on the nodejs code available invertx-redis-clientrepository:
If you don’t have a Redis service running locally, you can run Redis in a Docker container:
docker run --name redis -p 7006:6379 -d redis
Next, while being in vertx-redis-client root folder execute:
cd tools
npm i
npm start
The above sequence of commands should update the Command.java file, so it includes all the possible commands supported by a particular Redis version.
Command ZUNION = Command.create("zunion", -3, 0, 0, 0, false, true, true, false);
This definition is very important as we will have to use it in the service.
Once we have this Command we can start to update the redis-client extension by:
-
Updating the
RedisClientinterface, i.e.:
Response zunion(List<String> args);
-
Updating the
RedisClientImpl, i.e.:
@Override
public Response zunion(List<String> args) {
final io.vertx.mutiny.redis.client.Command ZUNION = Command.create("zunion", -3, 0, 0, 0, false, true, true, false);
final io.vertx.mutiny.redis.client.Request requestWithArgs = args.stream().reduce(
io.vertx.mutiny.redis.client.Request.cmd(ZUNION),
(request, s) -> request.arg(s),
(request, request2) -> request);
return await(mutinyRedis.send(requestWithArgs));
}
-
Updating the
ReactiveRedisClientinterface, i.e.:
Uni<Response> zunion(List<String> args);
Response zunionAndAwait(List<String> args);
-
Updating the
ReactiveRedisClientImpl, i.e.:
@Override
public Uni<Response> zunion(List<String> args) {
final Command ZUNION = Command.create("zunion", -3, 0, 0, 0, false, true, true, false);
final io.vertx.mutiny.redis.client.Request requestWithArgs = args.stream().reduce(
io.vertx.mutiny.redis.client.Request.cmd(ZUNION),
(request, s) -> request.arg(s),
(request, request2) -> request);
return mutinyRedis.send(requestWithArgs);
}
@Override
public Response zunionAndAwait(List<String> args) {
return zunion(args).await().indefinitely();
}
-
Please note that it’s using the
MutinyRedisclass which does asynchronous calls to Redis.