redis.clients.jedis.mcf.MultiClusterPipeline Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jedis_preview Show documentation
Show all versions of jedis_preview Show documentation
Jedis is a blazingly small and sane Redis java client.
The newest version!
package redis.clients.jedis.mcf;
import java.io.Closeable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import redis.clients.jedis.*;
import redis.clients.jedis.graph.ResultSet;
import redis.clients.jedis.providers.MultiClusterPooledConnectionProvider;
import redis.clients.jedis.util.KeyValue;
/**
* This is high memory dependent solution as all the appending commands will be hold in memory until
* {@link MultiClusterPipeline#sync() SYNC} (or {@link MultiClusterPipeline#close() CLOSE}) gets called.
*/
public class MultiClusterPipeline extends PipelineBase implements Closeable {
private final CircuitBreakerFailoverConnectionProvider failoverProvider;
private final Queue>> commands = new LinkedList<>();
public MultiClusterPipeline(MultiClusterPooledConnectionProvider pooledProvider) {
super(new CommandObjects());
this.failoverProvider = new CircuitBreakerFailoverConnectionProvider(pooledProvider);
try (Connection connection = failoverProvider.getConnection()) {
RedisProtocol proto = connection.getRedisProtocol();
if (proto != null) this.commandObjects.setProtocol(proto);
}
}
@Override
protected final Response appendCommand(CommandObject commandObject) {
CommandArguments args = commandObject.getArguments();
Response response = new Response<>(commandObject.getBuilder());
commands.add(KeyValue.of(args, response));
return response;
}
@Override
public void close() {
sync();
// connection prepared and closed (in try-with-resources) in sync()
}
/**
* Synchronize pipeline by reading all responses. This operation close the pipeline. In order to get return values
* from pipelined commands, capture the different Response<?> of the commands you execute.
*/
@Override
public void sync() {
if (commands.isEmpty()) return;
try (Connection connection = failoverProvider.getConnection()) {
commands.forEach((command) -> connection.sendCommand(command.getKey()));
// following connection.getMany(int) flushes anyway, so no flush here.
List