diff --git a/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisShardedTemplate.java b/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisShardedTemplate.java index 1616e86ce..e3a16d184 100644 --- a/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisShardedTemplate.java +++ b/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisShardedTemplate.java @@ -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; @@ -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 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) { diff --git a/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisTemplate.java b/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisTemplate.java index 5e448e263..db6193ced 100644 --- a/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisTemplate.java +++ b/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisTemplate.java @@ -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; @@ -22,12 +23,10 @@ /** * JedisTemplate 提供了一个template方法,负责对Jedis连接的获取与归还。 * JedisAction 和 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); @@ -38,6 +37,34 @@ public JedisTemplate(JedisPool jedisPool) { this.jedisPool = jedisPool; } + /** + * Callback interface for template. + */ + public interface JedisAction { + 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 action(Pipeline Pipeline); + } + + /** + * Callback interface for template without result. + */ + public interface PipelineActionNoResult { + void action(Pipeline Pipeline); + } + /** * Execute with a call back action with result. */ @@ -72,6 +99,44 @@ public void execute(JedisActionNoResult jedisAction) throws JedisException { } } + /** + * Execute with a call back action with result in pipeline. + */ + public List 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. */ @@ -115,20 +180,6 @@ protected void closeResource(Jedis jedis, boolean conectionBroken) { } - /** - * Callback interface for template. - */ - public interface JedisAction { - T action(Jedis jedis); - } - - /** - * Callback interface for template without result. - */ - public interface JedisActionNoResult { - void action(Jedis jedis); - } - // / Common Actions /// /**