avoid creating pipeline responses and do it lazy
This commit is contained in:
@@ -2015,10 +2015,19 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
return client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a pipeline, which is a very efficient way to send lots of command
|
||||
* and read all the responses when you finish sending them. Try to avoid
|
||||
* this version and use pipelined() when possible as it will give better
|
||||
* performance.
|
||||
*
|
||||
* @param jedisPipeline
|
||||
* @return The results of the command in the same order you've run them.
|
||||
*/
|
||||
public List<Object> pipelined(final PipelineBlock jedisPipeline) {
|
||||
jedisPipeline.setClient(client);
|
||||
jedisPipeline.execute();
|
||||
return jedisPipeline.sync();
|
||||
return jedisPipeline.syncAndReturnAll();
|
||||
}
|
||||
|
||||
public Pipeline pipelined() {
|
||||
|
||||
@@ -14,7 +14,28 @@ public class Pipeline extends Queable {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public List<Object> sync() {
|
||||
/**
|
||||
* Syncronize 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.
|
||||
*/
|
||||
public void sync() {
|
||||
List<Object> unformatted = client.getAll();
|
||||
for (Object o : unformatted) {
|
||||
generateResponse(o);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncronize pipeline by reading all responses. This operation close the
|
||||
* pipeline. Whenever possible try to avoid using this version and use
|
||||
* Pipeline.sync() as it won't go through all the responses and generate the
|
||||
* right response type (usually it is a waste of time).
|
||||
*
|
||||
* @return A list of all the responses in the order you executed them.
|
||||
* @see sync
|
||||
*/
|
||||
public List<Object> syncAndReturnAll() {
|
||||
List<Object> unformatted = client.getAll();
|
||||
List<Object> formatted = new ArrayList<Object>();
|
||||
for (Object o : unformatted) {
|
||||
|
||||
@@ -5,22 +5,29 @@ import redis.clients.jedis.exceptions.JedisDataException;
|
||||
public class Response<T> {
|
||||
protected T response = null;
|
||||
private boolean built = false;
|
||||
private boolean set = false;
|
||||
private Builder<T> builder;
|
||||
private Object data;
|
||||
|
||||
public Response(Builder<T> b) {
|
||||
this.builder = b;
|
||||
}
|
||||
|
||||
public void set(Object data) {
|
||||
response = builder.build(data);
|
||||
built = true;
|
||||
this.data = data;
|
||||
set = true;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
if (!built) {
|
||||
if (!set) {
|
||||
throw new JedisDataException(
|
||||
"Please close pipeline or multi block before calling this method.");
|
||||
}
|
||||
if (!built) {
|
||||
response = builder.build(data);
|
||||
this.data = null;
|
||||
built = true;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user