From bfe5ab672ebc42773385900a0899d8764afac526 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Mon, 8 Nov 2010 20:03:27 +0200 Subject: [PATCH] + add list implementations for Jedis and Jredis --- .../redis/connection/RedisCommands.java | 2 +- .../redis/connection/RedisListCommands.java | 24 +++ .../connection/jedis/JedisConnection.java | 192 +++++++++++++++--- .../connection/jredis/JredisConnection.java | 134 ++++++++++-- 4 files changed, 310 insertions(+), 42 deletions(-) diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java index f4cc407d7..f0f0e7502 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisCommands.java @@ -23,7 +23,7 @@ import java.util.Collection; * * @author Costin Leau */ -public interface RedisCommands extends RedisTxCommands, RedisStringCommands { +public interface RedisCommands extends RedisTxCommands, RedisStringCommands, RedisListCommands { Boolean exists(String key); diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java index dfb8a659e..bb2730005 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/RedisListCommands.java @@ -16,6 +16,8 @@ package org.springframework.datastore.redis.connection; +import java.util.List; + /** * List-specific commands supported by Redis. * @@ -26,4 +28,26 @@ public interface RedisListCommands { Integer rPush(String key, String value); Integer lPush(String key, String value); + + Integer lLen(String key); + + List lRange(String key, int start, int end); + + void lTrim(String key, int start, int end); + + String lIndex(String key, int index); + + void lSet(String key, int index, String value); + + Integer lRem(String key, int count, String value); + + String lPop(String key); + + String rPop(String key); + + List bLPop(int timeout, String... keys); + + List bRPop(int timeout, String... keys); + + String rPopLPush(String srcKey, String dstKey); } diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java index 091d57d13..9aea02464 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jedis/JedisConnection.java @@ -326,32 +326,6 @@ public class JedisConnection implements RedisConnection { } } - @Override - public Integer lPush(String key, String value) { - try { - if (isQueueing()) { - transaction.lpush(key, value); - return null; - } - return jedis.lpush(key, value); - } catch (Exception ex) { - throw convertJedisAccessException(ex); - } - } - - @Override - public Integer rPush(String key, String value) { - try { - if (isQueueing()) { - transaction.rpush(key, value); - return null; - } - return jedis.rpush(key, value); - } catch (Exception ex) { - throw convertJedisAccessException(ex); - } - } - @Override public String get(String key) { try { @@ -440,4 +414,170 @@ public class JedisConnection implements RedisConnection { throw convertJedisAccessException(ex); } } + + + @Override + public Integer lPush(String key, String value) { + try { + if (isQueueing()) { + transaction.lpush(key, value); + return null; + } + return jedis.lpush(key, value); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Integer rPush(String key, String value) { + try { + if (isQueueing()) { + transaction.rpush(key, value); + return null; + } + return jedis.rpush(key, value); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public List bLPop(int timeout, String... keys) { + try { + if (isQueueing()) { + throw new UnsupportedOperationException(); + } + return jedis.blpop(timeout, keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public List bRPop(int timeout, String... keys) { + try { + if (isQueueing()) { + throw new UnsupportedOperationException(); + } + return jedis.brpop(timeout, keys); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public String lIndex(String key, int index) { + try { + if (isQueueing()) { + transaction.lindex(key, index); + return null; + } + return jedis.lindex(key, index); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Integer lLen(String key) { + try { + if (isQueueing()) { + transaction.llen(key); + return null; + } + return jedis.llen(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public String lPop(String key) { + try { + if (isQueueing()) { + transaction.lpop(key); + return null; + } + return jedis.lpop(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public List lRange(String key, int start, int end) { + try { + if (isQueueing()) { + transaction.lrange(key, start, end); + return null; + } + return jedis.lrange(key, start, end); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public Integer lRem(String key, int count, String value) { + try { + if (isQueueing()) { + transaction.lrem(key, count, value); + return null; + } + return jedis.lrem(key, count, value); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public void lSet(String key, int index, String value) { + try { + if (isQueueing()) { + transaction.lset(key, index, value); + } + jedis.lset(key, index, value); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public void lTrim(String key, int start, int end) { + try { + if (isQueueing()) { + transaction.ltrim(key, start, end); + } + jedis.ltrim(key, start, end); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public String rPop(String key) { + try { + if (isQueueing()) { + transaction.rpop(key); + return null; + } + return jedis.lpop(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public String rPopLPush(String srcKey, String dstKey) { + try { + if (isQueueing()) { + transaction.rpoplpush(srcKey, dstKey); + return null; + } + return jedis.rpoplpush(srcKey, dstKey); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } } \ No newline at end of file diff --git a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java index d5a742beb..c09e97811 100644 --- a/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java +++ b/spring-datastore-redis/src/main/java/org/springframework/datastore/redis/connection/jredis/JredisConnection.java @@ -15,6 +15,7 @@ */ package org.springframework.datastore.redis.connection.jredis; +import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -162,21 +163,6 @@ public class JredisConnection implements RedisConnection { throw new UnsupportedOperationException(); } - @Override - public Integer lPush(String key, String value) { - try { - jredis.lpush(key, value); - return null; - } catch (RedisException ex) { - throw JredisUtils.convertJredisAccessException(ex); - } - } - - @Override - public Integer rPush(String key, String value) { - throw new UnsupportedOperationException(); - } - @Override public String get(String key) { try { @@ -239,4 +225,122 @@ public class JredisConnection implements RedisConnection { throw JredisUtils.convertJredisAccessException(ex); } } + + @Override + public List bLPop(int timeout, String... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public List bRPop(int timeout, String... keys) { + throw new UnsupportedOperationException(); + } + + @Override + public String lIndex(String key, int index) { + try { + return JredisUtils.convertToString(jredis.lindex(key, (long) index), encoding); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public Integer lLen(String key) { + try { + return Integer.valueOf((int) jredis.llen(key)); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public String lPop(String key) { + try { + return JredisUtils.convertToString(jredis.lpop(key), encoding); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public Integer lPush(String key, String value) { + try { + jredis.lpush(key, value); + return null; + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public List lRange(String key, int start, int end) { + try { + List lrange = jredis.lrange(key, start, end); + List results = new ArrayList(lrange.size()); + + for (byte[] bs : lrange) { + results.add(JredisUtils.convertToString(bs, encoding)); + } + return results; + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public Integer lRem(String key, int count, String value) { + try { + Integer.valueOf((int) jredis.lrem(key, value, count)); + return null; + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public void lSet(String key, int index, String value) { + try { + jredis.lset(key, index, value); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public void lTrim(String key, int start, int end) { + try { + jredis.ltrim(key, start, end); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public String rPop(String key) { + try { + return JredisUtils.convertToString(jredis.rpop(key), encoding); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public String rPopLPush(String srcKey, String dstKey) { + try { + return JredisUtils.convertToString(jredis.rpoplpush(srcKey, dstKey), encoding); + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } + + @Override + public Integer rPush(String key, String value) { + try { + jredis.rpush(key, value); + return null; + } catch (RedisException ex) { + throw JredisUtils.convertJredisAccessException(ex); + } + } } \ No newline at end of file