Un-reinvent the clustered script execution wheel.

This commit is contained in:
Jon Chambers 2020-06-17 22:11:37 -04:00 committed by Jon Chambers
parent e9b0100b06
commit 26a03b55de
1 changed files with 2 additions and 10 deletions

View File

@ -46,20 +46,12 @@ public class ClusterLuaScript {
public Object execute(final List<String> keys, final List<String> args) {
return redisCluster.withWriteCluster(connection -> {
final RedisAdvancedClusterCommands<String, String> clusterCommands = connection.sync();
final RedisCommands<String, String> redisCommands;
if (keys.isEmpty()) {
redisCommands = clusterCommands.masters().commands(0);
} else {
final int slot = SlotHash.getSlot(keys.get(0));
redisCommands = clusterCommands.nodes(node -> node.is(RedisClusterNode.NodeFlag.MASTER) && node.hasSlot(slot)).commands(0);
}
try {
return redisCommands.evalsha(sha, scriptOutputType, keys.toArray(STRING_ARRAY), args.toArray(STRING_ARRAY));
return clusterCommands.evalsha(sha, scriptOutputType, keys.toArray(STRING_ARRAY), args.toArray(STRING_ARRAY));
} catch (final RedisNoScriptException e) {
clusterCommands.scriptLoad(script);
return redisCommands.evalsha(sha, scriptOutputType, keys.toArray(STRING_ARRAY), args.toArray(STRING_ARRAY));
return clusterCommands.evalsha(sha, scriptOutputType, keys.toArray(STRING_ARRAY), args.toArray(STRING_ARRAY));
}
});
}