avoid creating pipeline responses and do it lazy

This commit is contained in:
Jonathan Leibiusky
2011-05-30 13:43:21 -03:00
parent 44c3eef60e
commit d690833ed6
5 changed files with 51 additions and 16 deletions

View File

@@ -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() {

View File

@@ -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) {

View File

@@ -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;
}