Skip to content

Commit

Permalink
#393 增加Pipeline内运行的回调函数接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin committed Sep 14, 2014
1 parent f3d8434 commit 6af30e2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import org.springside.modules.nosql.redis.JedisTemplate.JedisAction;
import org.springside.modules.nosql.redis.JedisTemplate.JedisActionNoResult;
import org.springside.modules.nosql.redis.JedisTemplate.PipelineAction;
import org.springside.modules.nosql.redis.JedisTemplate.PipelineActionNoResult;
import org.springside.modules.nosql.redis.pool.JedisPool;

import redis.clients.jedis.Tuple;
Expand Down Expand Up @@ -90,6 +92,22 @@ public void execute(String key, JedisActionNoResult jedisAction) throws JedisExc
jedisTemplate.execute(jedisAction);
}

/*
* Execute the action, the action must process only one key.
*/
public List<Object> execute(String key, PipelineAction pipelineAction) throws JedisException {
JedisTemplate jedisTemplate = getShard(key);
return jedisTemplate.execute(pipelineAction);
}

/*
* Execute the action, the action must process only one key.
*/
public void execute(String key, PipelineActionNoResult pipelineAction) throws JedisException {
JedisTemplate jedisTemplate = getShard(key);
jedisTemplate.execute(pipelineAction);
}

// / Common Actions ///

public Boolean del(final String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springside.modules.nosql.redis.pool.JedisPool;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
Expand All @@ -22,12 +23,10 @@
/**
* JedisTemplate 提供了一个template方法,负责对Jedis连接的获取与归还。
* JedisAction<T> 和 JedisActionNoResult两种回调接口,适用于有无返回值两种情况。
* PipelineAction 与 PipelineActionResult两种接口,适合于pipeline中批量传输命令的情况。
*
* 同时提供一些JedisOperation中定义的 最常用函数的封装, 如get/set/zadd等。
*/

/**
* JedisTemplate is the template to execute jedis actions, fetch and return the connection from the pool correctly.
*/
public class JedisTemplate {

private static Logger logger = LoggerFactory.getLogger(JedisTemplate.class);
Expand All @@ -38,6 +37,34 @@ public JedisTemplate(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}

/**
* Callback interface for template.
*/
public interface JedisAction<T> {
T action(Jedis jedis);
}

/**
* Callback interface for template without result.
*/
public interface JedisActionNoResult {
void action(Jedis jedis);
}

/**
* Callback interface for template.
*/
public interface PipelineAction {
List<Object> action(Pipeline Pipeline);
}

/**
* Callback interface for template without result.
*/
public interface PipelineActionNoResult {
void action(Pipeline Pipeline);
}

/**
* Execute with a call back action with result.
*/
Expand Down Expand Up @@ -72,6 +99,44 @@ public void execute(JedisActionNoResult jedisAction) throws JedisException {
}
}

/**
* Execute with a call back action with result in pipeline.
*/
public List<Object> execute(PipelineAction pipelineAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
Pipeline pipeline = jedis.pipelined();
pipelineAction.action(pipeline);
return pipeline.syncAndReturnAll();
} catch (JedisException e) {
broken = handleJedisException(e);
throw e;
} finally {
closeResource(jedis, broken);
}
}

/**
* Execute with a call back action without result in pipeline.
*/
public void execute(PipelineActionNoResult pipelineAction) throws JedisException {
Jedis jedis = null;
boolean broken = false;
try {
jedis = jedisPool.getResource();
Pipeline pipeline = jedis.pipelined();
pipelineAction.action(pipeline);
pipeline.sync();
} catch (JedisException e) {
broken = handleJedisException(e);
throw e;
} finally {
closeResource(jedis, broken);
}
}

/**
* Return the internal JedisPool.
*/
Expand Down Expand Up @@ -115,20 +180,6 @@ protected void closeResource(Jedis jedis, boolean conectionBroken) {

}

/**
* Callback interface for template.
*/
public interface JedisAction<T> {
T action(Jedis jedis);
}

/**
* Callback interface for template without result.
*/
public interface JedisActionNoResult {
void action(Jedis jedis);
}

// / Common Actions ///

/**
Expand Down

0 comments on commit 6af30e2

Please sign in to comment.