Skip to content

Commit

Permalink
make Command.create() return existing instance for known commands
Browse files Browse the repository at this point in the history
When the `Command.create()` method is used to create a `Command` instance
for a known command (not recommended, but also not forbidden), it used to
return a generic instance that doesn't know anything about where the keys
are in the command. Such generic instance is unusable with Redis cluster,
because the target node will be selected randomly, not based on the key,
and there's high chance such command will result in the `MOVED` redirect.

With this commit, `Command.create()` will return a pre-existing static
instance for known commands, which is key-aware and works with Redis cluster.
  • Loading branch information
Ladicek authored and vietj committed Nov 13, 2023
1 parent fe2ae38 commit 727eb5a
Show file tree
Hide file tree
Showing 4 changed files with 462 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/main/java/io/vertx/redis/client/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* <b>Auto generated</b> API Commands to interact with REDIS.
*
* @author <a href="mailto:[email protected]">Paulo Lopes</a>
* @version redis_version:7.0.6
* @version redis_version:7.0.12
*/
@VertxGen
public interface Command {
Expand Down Expand Up @@ -427,11 +427,15 @@ public interface Command {

/**
* Generic command generator for extensions.
* <p>
* To avoid inconsistent behavior, when {@code command} is one of the known commands
* for which a static instance exists, the static instance is returned.
*
* @param command command name
* @return the cacheable immutable command instance
*/
static Command create(String command) {
return new CommandImpl(command, -1, null, false, true);
Command known = CommandMap.getKnownCommand(command);
return known != null ? known : new CommandImpl(command, -1, null, false, true);
}
}
Loading

0 comments on commit 727eb5a

Please sign in to comment.