Upgrade to JRedis master

DATAREDIS-54, DATAREDIS-174, DATAREDIS-129

- Switch to original JRedis for 2.6 support

- Remove base64 encoding as new JRedis supports binary
keys

- Add JredisPool interface to replace removed
JRedisService and pool implementation

- Temporarily switch integration tests to use
non-pooled connections

- Throw more specific Exception on JRedis connection
failures
This commit is contained in:
Jennifer Hickey
2013-06-11 15:39:42 -07:00
parent dffa1de6d1
commit 280e6a6ccc
14 changed files with 228 additions and 269 deletions

View File

@@ -31,7 +31,6 @@ import org.jredis.Query.Support;
import org.jredis.RedisException;
import org.jredis.Sort;
import org.jredis.protocol.Command;
import org.jredis.ri.alphazero.JRedisService;
import org.jredis.ri.alphazero.JRedisSupport;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.RedisSystemException;
@@ -48,14 +47,17 @@ import org.springframework.util.ReflectionUtils;
* {@code RedisConnection} implementation on top of <a href="http://github.com/alphazero/jredis">JRedis</a> library.
*
* @author Costin Leau
* @author Jennifer Hickey
*/
public class JredisConnection implements RedisConnection {
private static final Method SERVICE_REQUEST;
private final JRedis jredis;
private final boolean isPool;
private final JredisPool pool;
private boolean isClosed = false;
/** flag indicating whether the connection needs to be dropped or not */
private boolean broken = false;
static {
SERVICE_REQUEST = ReflectionUtils.findMethod(JRedisSupport.class, "serviceRequest", Command.class,
@@ -69,13 +71,17 @@ public class JredisConnection implements RedisConnection {
* @param jredis JRedis connection
*/
public JredisConnection(JRedis jredis) {
this(jredis, null);
}
public JredisConnection(JRedis jredis, JredisPool pool) {
Assert.notNull(jredis, "a not-null instance required");
this.jredis = jredis;
// required since Jredis combines the pool and the connection under the same interface/class
this.isPool = (jredis instanceof JRedisService);
this.pool = pool;
}
protected DataAccessException convertJredisAccessException(Exception ex) {
broken = true;
if (ex instanceof RedisException) {
return JredisUtils.convertJredisAccessException((RedisException) ex);
}
@@ -105,17 +111,22 @@ public class JredisConnection implements RedisConnection {
public void close() throws RedisSystemException {
isClosed = true;
// don't actually close the connection
// if a pool is used
if (!isPool) {
try {
jredis.quit();
} catch (Exception ex) {
throw convertJredisAccessException(ex);
if (pool != null) {
if (!broken) {
pool.returnResource(jredis);
}else {
pool.returnBrokenResource(jredis);
}
return;
}
}
try {
jredis.quit();
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
}
public JRedis getNativeConnection() {
return jredis;
@@ -148,7 +159,7 @@ public class JredisConnection implements RedisConnection {
public List<byte[]> sort(byte[] key, SortParameters params) {
Sort sort = jredis.sort(JredisUtils.decode(key));
Sort sort = jredis.sort(key);
JredisUtils.applySortingParams(sort, params, null);
try {
return sort.exec();
@@ -159,7 +170,7 @@ public class JredisConnection implements RedisConnection {
public Long sort(byte[] key, SortParameters params, byte[] storeKey) {
Sort sort = jredis.sort(JredisUtils.decode(key));
Sort sort = jredis.sort(key);
JredisUtils.applySortingParams(sort, params, storeKey);
try {
return Support.unpackValue(sort.exec());
@@ -171,7 +182,7 @@ public class JredisConnection implements RedisConnection {
public Long dbSize() {
try {
return jredis.dbsize();
return (Long)jredis.dbsize();
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -258,7 +269,7 @@ public class JredisConnection implements RedisConnection {
public Long lastSave() {
try {
return jredis.lastsave();
return (Long)jredis.lastsave();
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -282,7 +293,7 @@ public class JredisConnection implements RedisConnection {
public Long del(byte[]... keys) {
try {
return jredis.del(JredisUtils.decodeMultiple(keys));
return jredis.del(keys);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -305,7 +316,7 @@ public class JredisConnection implements RedisConnection {
public Boolean exists(byte[] key) {
try {
return jredis.exists(JredisUtils.decode(key));
return jredis.exists(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -314,7 +325,7 @@ public class JredisConnection implements RedisConnection {
public Boolean expire(byte[] key, long seconds) {
try {
return jredis.expire(JredisUtils.decode(key), (int) seconds);
return jredis.expire(key, (int) seconds);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -323,7 +334,7 @@ public class JredisConnection implements RedisConnection {
public Boolean expireAt(byte[] key, long unixTime) {
try {
return jredis.expireat(JredisUtils.decode(key), unixTime);
return jredis.expireat(key, unixTime);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -332,7 +343,7 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> keys(byte[] pattern) {
try {
return JredisUtils.convertToSet(jredis.keys(JredisUtils.decode(pattern)));
return new LinkedHashSet<byte[]>(jredis.keys(pattern));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -352,7 +363,7 @@ public class JredisConnection implements RedisConnection {
public Boolean move(byte[] key, int dbIndex) {
try {
return jredis.move(JredisUtils.decode(key), dbIndex);
return jredis.move(key, dbIndex);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -361,7 +372,7 @@ public class JredisConnection implements RedisConnection {
public byte[] randomKey() {
try {
return JredisUtils.encode(jredis.randomkey());
return jredis.randomkey();
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -370,7 +381,7 @@ public class JredisConnection implements RedisConnection {
public void rename(byte[] oldName, byte[] newName) {
try {
jredis.rename(JredisUtils.decode(oldName), JredisUtils.decode(newName));
jredis.rename(oldName, newName);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -379,7 +390,7 @@ public class JredisConnection implements RedisConnection {
public Boolean renameNX(byte[] oldName, byte[] newName) {
try {
return jredis.renamenx(JredisUtils.decode(oldName), JredisUtils.decode(newName));
return jredis.renamenx(oldName, newName);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -393,7 +404,7 @@ public class JredisConnection implements RedisConnection {
public Long ttl(byte[] key) {
try {
return jredis.ttl(JredisUtils.decode(key));
return jredis.ttl(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -402,7 +413,7 @@ public class JredisConnection implements RedisConnection {
public DataType type(byte[] key) {
try {
return JredisUtils.convertDataType(jredis.type(JredisUtils.decode(key)));
return JredisUtils.convertDataType(jredis.type(key));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -425,7 +436,7 @@ public class JredisConnection implements RedisConnection {
public byte[] get(byte[] key) {
try {
return jredis.get(JredisUtils.decode(key));
return jredis.get(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -434,7 +445,7 @@ public class JredisConnection implements RedisConnection {
public void set(byte[] key, byte[] value) {
try {
jredis.set(JredisUtils.decode(key), value);
jredis.set(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -443,7 +454,7 @@ public class JredisConnection implements RedisConnection {
public byte[] getSet(byte[] key, byte[] value) {
try {
return jredis.getset(JredisUtils.decode(key), value);
return jredis.getset(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -452,7 +463,7 @@ public class JredisConnection implements RedisConnection {
public Long append(byte[] key, byte[] value) {
try {
return jredis.append(JredisUtils.decode(key), value);
return jredis.append(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -461,7 +472,7 @@ public class JredisConnection implements RedisConnection {
public List<byte[]> mGet(byte[]... keys) {
try {
return jredis.mget(JredisUtils.decodeMultiple(keys));
return jredis.mget(keys);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -470,7 +481,7 @@ public class JredisConnection implements RedisConnection {
public void mSet(Map<byte[], byte[]> tuple) {
try {
jredis.mset(JredisUtils.decodeMap(tuple));
jredis.mset(tuple);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -479,7 +490,7 @@ public class JredisConnection implements RedisConnection {
public void mSetNX(Map<byte[], byte[]> tuple) {
try {
jredis.msetnx(JredisUtils.decodeMap(tuple));
jredis.msetnx(tuple);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -493,7 +504,7 @@ public class JredisConnection implements RedisConnection {
public Boolean setNX(byte[] key, byte[] value) {
try {
return jredis.setnx(JredisUtils.decode(key), value);
return jredis.setnx(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -502,7 +513,7 @@ public class JredisConnection implements RedisConnection {
public byte[] getRange(byte[] key, long start, long end) {
try {
return jredis.substr(JredisUtils.decode(key), start, end);
return jredis.substr(key, start, end);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -511,7 +522,7 @@ public class JredisConnection implements RedisConnection {
public Long decr(byte[] key) {
try {
return jredis.decr(JredisUtils.decode(key));
return jredis.decr(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -520,7 +531,7 @@ public class JredisConnection implements RedisConnection {
public Long decrBy(byte[] key, long value) {
try {
return jredis.decrby(JredisUtils.decode(key), (int) value);
return jredis.decrby(key, (int) value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -529,7 +540,7 @@ public class JredisConnection implements RedisConnection {
public Long incr(byte[] key) {
try {
return jredis.incr(JredisUtils.decode(key));
return jredis.incr(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -538,7 +549,7 @@ public class JredisConnection implements RedisConnection {
public Long incrBy(byte[] key, long value) {
try {
return jredis.incrby(JredisUtils.decode(key), (int) value);
return jredis.incrby(key, (int) value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -546,12 +557,20 @@ public class JredisConnection implements RedisConnection {
public Boolean getBit(byte[] key, long offset) {
throw new UnsupportedOperationException();
try {
return jredis.getbit(key, (int)offset);
} catch(Exception ex) {
throw convertJredisAccessException(ex);
}
}
public void setBit(byte[] key, long offset, boolean value) {
throw new UnsupportedOperationException();
try {
jredis.setbit(key, (int) offset, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
}
@@ -581,7 +600,7 @@ public class JredisConnection implements RedisConnection {
public byte[] lIndex(byte[] key, long index) {
try {
return jredis.lindex(JredisUtils.decode(key), index);
return jredis.lindex(key, index);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -590,7 +609,7 @@ public class JredisConnection implements RedisConnection {
public Long lLen(byte[] key) {
try {
return jredis.llen(JredisUtils.decode(key));
return jredis.llen(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -599,7 +618,7 @@ public class JredisConnection implements RedisConnection {
public byte[] lPop(byte[] key) {
try {
return jredis.lpop(JredisUtils.decode(key));
return jredis.lpop(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -608,7 +627,7 @@ public class JredisConnection implements RedisConnection {
public Long lPush(byte[] key, byte[] value) {
try {
jredis.lpush(JredisUtils.decode(key), value);
jredis.lpush(key, value);
return null;
} catch (Exception ex) {
throw convertJredisAccessException(ex);
@@ -618,7 +637,7 @@ public class JredisConnection implements RedisConnection {
public List<byte[]> lRange(byte[] key, long start, long end) {
try {
List<byte[]> lrange = jredis.lrange(JredisUtils.decode(key), start, end);
List<byte[]> lrange = jredis.lrange(key, start, end);
return lrange;
} catch (Exception ex) {
@@ -629,7 +648,7 @@ public class JredisConnection implements RedisConnection {
public Long lRem(byte[] key, long count, byte[] value) {
try {
return jredis.lrem(JredisUtils.decode(key), value, (int) count);
return jredis.lrem(key, value, (int) count);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -638,7 +657,7 @@ public class JredisConnection implements RedisConnection {
public void lSet(byte[] key, long index, byte[] value) {
try {
jredis.lset(JredisUtils.decode(key), index, value);
jredis.lset(key, index, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -647,7 +666,7 @@ public class JredisConnection implements RedisConnection {
public void lTrim(byte[] key, long start, long end) {
try {
jredis.ltrim(JredisUtils.decode(key), start, end);
jredis.ltrim(key, start, end);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -656,7 +675,7 @@ public class JredisConnection implements RedisConnection {
public byte[] rPop(byte[] key) {
try {
return jredis.rpop(JredisUtils.decode(key));
return jredis.rpop(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -665,7 +684,7 @@ public class JredisConnection implements RedisConnection {
public byte[] rPopLPush(byte[] srcKey, byte[] dstKey) {
try {
return jredis.rpoplpush(JredisUtils.decode(srcKey), JredisUtils.decode(dstKey));
return jredis.rpoplpush(srcKey, dstKey);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -674,7 +693,7 @@ public class JredisConnection implements RedisConnection {
public Long rPush(byte[] key, byte[] value) {
try {
jredis.rpush(JredisUtils.decode(key), value);
jredis.rpush(key, value);
return null;
} catch (Exception ex) {
throw convertJredisAccessException(ex);
@@ -709,7 +728,7 @@ public class JredisConnection implements RedisConnection {
public Boolean sAdd(byte[] key, byte[] value) {
try {
return jredis.sadd(JredisUtils.decode(key), value);
return jredis.sadd(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -718,7 +737,7 @@ public class JredisConnection implements RedisConnection {
public Long sCard(byte[] key) {
try {
return jredis.scard(JredisUtils.decode(key));
return jredis.scard(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -726,8 +745,8 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> sDiff(byte[]... keys) {
String destKey = JredisUtils.decode(keys[0]);
String[] sets = JredisUtils.decodeMultiple(Arrays.copyOfRange(keys, 1, keys.length));
byte[] destKey = keys[0];
byte[][] sets = Arrays.copyOfRange(keys, 1, keys.length);
try {
List<byte[]> result = jredis.sdiff(destKey, sets);
@@ -739,11 +758,8 @@ public class JredisConnection implements RedisConnection {
public Long sDiffStore(byte[] destKey, byte[]... keys) {
String destSet = JredisUtils.decode(destKey);
String[] sets = JredisUtils.decodeMultiple(keys);
try {
jredis.sdiffstore(destSet, sets);
jredis.sdiffstore(destKey, keys);
return Long.valueOf(-1);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
@@ -752,8 +768,8 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> sInter(byte[]... keys) {
String set1 = JredisUtils.decode(keys[0]);
String[] sets = JredisUtils.decodeMultiple(Arrays.copyOfRange(keys, 1, keys.length));
byte[] set1 = keys[0];
byte[][] sets = Arrays.copyOfRange(keys, 1, keys.length);
try {
List<byte[]> result = jredis.sinter(set1, sets);
@@ -765,11 +781,8 @@ public class JredisConnection implements RedisConnection {
public Long sInterStore(byte[] destKey, byte[]... keys) {
String destSet = JredisUtils.decode(destKey);
String[] sets = JredisUtils.decodeMultiple(keys);
try {
jredis.sinterstore(destSet, sets);
jredis.sinterstore(destKey, keys);
return Long.valueOf(-1);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
@@ -779,7 +792,7 @@ public class JredisConnection implements RedisConnection {
public Boolean sIsMember(byte[] key, byte[] value) {
try {
return jredis.sismember(JredisUtils.decode(key), value);
return jredis.sismember(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -788,7 +801,7 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> sMembers(byte[] key) {
try {
return new LinkedHashSet<byte[]>(jredis.smembers(JredisUtils.decode(key)));
return new LinkedHashSet<byte[]>(jredis.smembers(key));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -797,7 +810,7 @@ public class JredisConnection implements RedisConnection {
public Boolean sMove(byte[] srcKey, byte[] destKey, byte[] value) {
try {
return jredis.smove(JredisUtils.decode(srcKey), JredisUtils.decode(destKey), value);
return jredis.smove(srcKey, destKey, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -806,7 +819,7 @@ public class JredisConnection implements RedisConnection {
public byte[] sPop(byte[] key) {
try {
return jredis.spop(JredisUtils.decode(key));
return jredis.spop(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -815,7 +828,7 @@ public class JredisConnection implements RedisConnection {
public byte[] sRandMember(byte[] key) {
try {
return jredis.srandmember(JredisUtils.decode(key));
return jredis.srandmember(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -824,7 +837,7 @@ public class JredisConnection implements RedisConnection {
public Boolean sRem(byte[] key, byte[] value) {
try {
return jredis.srem(JredisUtils.decode(key), value);
return jredis.srem(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -832,8 +845,8 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> sUnion(byte[]... keys) {
String set1 = JredisUtils.decode(keys[0]);
String[] sets = JredisUtils.decodeMultiple(Arrays.copyOfRange(keys, 1, keys.length));
byte[] set1 = keys[0];
byte[][] sets = Arrays.copyOfRange(keys, 1, keys.length);
try {
return new LinkedHashSet<byte[]>(jredis.sunion(set1, sets));
@@ -844,11 +857,8 @@ public class JredisConnection implements RedisConnection {
public Long sUnionStore(byte[] destKey, byte[]... keys) {
String destSet = JredisUtils.decode(destKey);
String[] sets = JredisUtils.decodeMultiple(keys);
try {
jredis.sunionstore(destSet, sets);
jredis.sunionstore(destKey, keys);
return Long.valueOf(-1);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
@@ -863,7 +873,7 @@ public class JredisConnection implements RedisConnection {
public Boolean zAdd(byte[] key, double score, byte[] value) {
try {
return jredis.zadd(JredisUtils.decode(key), score, value);
return jredis.zadd(key, score, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -872,7 +882,7 @@ public class JredisConnection implements RedisConnection {
public Long zCard(byte[] key) {
try {
return jredis.zcard(JredisUtils.decode(key));
return jredis.zcard(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -881,7 +891,7 @@ public class JredisConnection implements RedisConnection {
public Long zCount(byte[] key, double min, double max) {
try {
return jredis.zcount(JredisUtils.decode(key), min, max);
return jredis.zcount(key, min, max);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -890,7 +900,7 @@ public class JredisConnection implements RedisConnection {
public Double zIncrBy(byte[] key, double increment, byte[] value) {
try {
return jredis.zincrby(JredisUtils.decode(key), increment, value);
return jredis.zincrby(key, increment, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -909,7 +919,7 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> zRange(byte[] key, long start, long end) {
try {
return new LinkedHashSet<byte[]>(jredis.zrange(JredisUtils.decode(key), start, end));
return new LinkedHashSet<byte[]>(jredis.zrange(key, start, end));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -923,7 +933,7 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> zRangeByScore(byte[] key, double min, double max) {
try {
return new LinkedHashSet<byte[]>(jredis.zrangebyscore(JredisUtils.decode(key), min, max));
return new LinkedHashSet<byte[]>(jredis.zrangebyscore(key, min, max));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -967,7 +977,7 @@ public class JredisConnection implements RedisConnection {
public Long zRank(byte[] key, byte[] value) {
try {
return jredis.zrank(JredisUtils.decode(key), value);
return jredis.zrank(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -976,7 +986,7 @@ public class JredisConnection implements RedisConnection {
public Boolean zRem(byte[] key, byte[] value) {
try {
return jredis.zrem(JredisUtils.decode(key), value);
return jredis.zrem(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -985,7 +995,7 @@ public class JredisConnection implements RedisConnection {
public Long zRemRange(byte[] key, long start, long end) {
try {
return jredis.zremrangebyrank(JredisUtils.decode(key), start, end);
return jredis.zremrangebyrank(key, start, end);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -994,7 +1004,7 @@ public class JredisConnection implements RedisConnection {
public Long zRemRangeByScore(byte[] key, double min, double max) {
try {
return jredis.zremrangebyscore(JredisUtils.decode(key), min, max);
return jredis.zremrangebyscore(key, min, max);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1003,7 +1013,7 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> zRevRange(byte[] key, long start, long end) {
try {
return new LinkedHashSet<byte[]>(jredis.zrevrange(JredisUtils.decode(key), start, end));
return new LinkedHashSet<byte[]>(jredis.zrevrange(key, start, end));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1017,7 +1027,7 @@ public class JredisConnection implements RedisConnection {
public Long zRevRank(byte[] key, byte[] value) {
try {
return jredis.zrevrank(JredisUtils.decode(key), value);
return jredis.zrevrank(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1026,7 +1036,7 @@ public class JredisConnection implements RedisConnection {
public Double zScore(byte[] key, byte[] value) {
try {
return jredis.zscore(JredisUtils.decode(key), value);
return jredis.zscore(key, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1050,7 +1060,7 @@ public class JredisConnection implements RedisConnection {
public Boolean hDel(byte[] key, byte[] field) {
try {
return jredis.hdel(JredisUtils.decode(key), JredisUtils.decode(field));
return jredis.hdel(key, field);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1059,7 +1069,7 @@ public class JredisConnection implements RedisConnection {
public Boolean hExists(byte[] key, byte[] field) {
try {
return jredis.hexists(JredisUtils.decode(key), JredisUtils.decode(field));
return jredis.hexists(key, field);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1068,7 +1078,7 @@ public class JredisConnection implements RedisConnection {
public byte[] hGet(byte[] key, byte[] field) {
try {
return jredis.hget(JredisUtils.decode(key), JredisUtils.decode(field));
return jredis.hget(key, field);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1077,7 +1087,7 @@ public class JredisConnection implements RedisConnection {
public Map<byte[], byte[]> hGetAll(byte[] key) {
try {
return JredisUtils.encodeMap(jredis.hgetall(JredisUtils.decode(key)));
return jredis.hgetall(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1091,7 +1101,7 @@ public class JredisConnection implements RedisConnection {
public Set<byte[]> hKeys(byte[] key) {
try {
return new LinkedHashSet<byte[]>(JredisUtils.convertToSet(jredis.hkeys(JredisUtils.decode(key))));
return new LinkedHashSet<byte[]>(jredis.hkeys(key));
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1100,7 +1110,7 @@ public class JredisConnection implements RedisConnection {
public Long hLen(byte[] key) {
try {
return jredis.hlen(JredisUtils.decode(key));
return jredis.hlen(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1119,7 +1129,7 @@ public class JredisConnection implements RedisConnection {
public Boolean hSet(byte[] key, byte[] field, byte[] value) {
try {
return jredis.hset(JredisUtils.decode(key), JredisUtils.decode(field), value);
return jredis.hset(key, field, value);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}
@@ -1133,7 +1143,7 @@ public class JredisConnection implements RedisConnection {
public List<byte[]> hVals(byte[] key) {
try {
return jredis.hvals(JredisUtils.decode(key));
return jredis.hvals(key);
} catch (Exception ex) {
throw convertJredisAccessException(ex);
}

View File

@@ -17,10 +17,9 @@ package org.springframework.data.redis.connection.jredis;
import org.jredis.ClientRuntimeException;
import org.jredis.connector.Connection;
import org.jredis.connector.ConnectionSpec;
import org.jredis.connector.Connection.Socket.Property;
import org.jredis.connector.ConnectionSpec;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.JRedisService;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
@@ -34,6 +33,7 @@ import org.springframework.util.StringUtils;
* Connection factory using creating <a href="http://github.com/alphazero/jredis">JRedis</a> based connections.
*
* @author Costin Leau
* @author Jennifer Hickey
*/
public class JredisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
@@ -43,13 +43,8 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
private int port = DEFAULT_REDIS_PORT;
private String password = null;
private int timeout;
private boolean usePool = true;
private int dbIndex = DEFAULT_REDIS_DB;
private JRedisService pool = null;
// taken from JRedis code
private int poolSize = 5;
private JredisPool pool;
private static final int DEFAULT_REDIS_PORT = 6379;
private static final int DEFAULT_REDIS_DB = 0;
@@ -71,10 +66,13 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
public JredisConnectionFactory(ConnectionSpec connectionSpec) {
this.connectionSpec = connectionSpec;
}
public JredisConnectionFactory(JredisPool pool) {
this.pool = pool;
}
public void afterPropertiesSet() {
if (connectionSpec == null) {
if (connectionSpec == null && pool == null) {
Assert.hasText(hostName);
connectionSpec = DefaultConnectionSpec.newSpec(hostName, port, dbIndex, DEFAULT_REDIS_PASSWORD);
connectionSpec.setConnectionFlag(Connection.Flag.RELIABLE, false);
@@ -87,29 +85,25 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
connectionSpec.setSocketProperty(Property.SO_TIMEOUT, timeout);
}
}
if (usePool) {
int size = getPoolSize();
pool = new JRedisService(connectionSpec, size);
}
}
public void destroy() {
if (usePool && pool != null) {
pool.quit();
public void destroy() throws Exception {
if(pool != null) {
pool.destroy();
pool = null;
}
}
public RedisConnection getConnection() {
return postProcessConnection(new JredisConnection((usePool ? pool : new JRedisClient(connectionSpec))));
JredisConnection connection;
if(pool != null) {
connection = new JredisConnection(pool.getResource(), pool);
}else {
connection = new JredisConnection(new JRedisClient(connectionSpec), null);
}
return postProcessConnection(connection);
}
/**
* Post process a newly retrieved connection. Useful for decorating or executing
* initialization commands on a new connection.
@@ -186,44 +180,6 @@ public class JredisConnectionFactory implements InitializingBean, DisposableBean
this.password = password;
}
/**
* Indicates the use of a connection pool.
*
* @return Returns the use of connection pooling.
*/
public boolean getUsePool() {
return usePool;
}
/**
* Turns on or off the use of connection pooling.
*
* @param usePool The usePool to set.
*/
public void setUsePool(boolean usePool) {
this.usePool = usePool;
}
/**
* Returns the pool size of this factory.
*
* @return Returns the poolSize
*/
public int getPoolSize() {
return poolSize;
}
/**
* Sets the connection pool size of the underlying factory.
*
* @param poolSize The poolSize to set.
*/
public void setPoolSize(int poolSize) {
Assert.isTrue(poolSize > 0, "pool size needs to be bigger then zero");
this.poolSize = poolSize;
usePool = true;
}
/**
* Returns the index of the database.
*

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2011-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.jredis;
import org.jredis.ClientRuntimeException;
import org.jredis.JRedis;
/**
* Pool of {@link JRedis} connection objects.
*
* @author Jennifer Hickey
*
*/
public interface JredisPool {
/**
*
* @return A {@link JRedis} connection, if available. Throws a
* {@link ClientRuntimeException} if a connection cannot be borrowed
* from the pool
*/
JRedis getResource();
/**
*
* @param resource
* A broken {@link JRedis} connection that should be invalidated
*/
void returnBrokenResource(final JRedis resource);
/**
*
* @param resource
* A {@link JRedis} connection to return to the pool
*/
void returnResource(final JRedis resource);
/**
* Destroys the connection pool
*/
void destroy();
}

View File

@@ -16,28 +16,28 @@
package org.springframework.data.redis.connection.jredis;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.jredis.ClientRuntimeException;
import org.jredis.RedisException;
import org.jredis.RedisType;
import org.jredis.Sort;
import org.jredis.connector.NotConnectedException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.connection.SortParameters.Order;
import org.springframework.data.redis.connection.SortParameters.Range;
import org.springframework.data.redis.connection.util.DecodeUtils;
/**
* Helper class featuring methods for JRedis connection handling, providing support for exception translation.
*
* @author Costin Leau
* @author Jennifer Hickey
*/
public abstract class JredisUtils {
@@ -58,6 +58,9 @@ public abstract class JredisUtils {
* @return converted exception
*/
public static DataAccessException convertJredisAccessException(ClientRuntimeException ex) {
if (ex instanceof NotConnectedException) {
return new RedisConnectionFailureException(ex.getMessage(), ex);
}
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
}
@@ -80,41 +83,17 @@ public abstract class JredisUtils {
return null;
}
static String decode(byte[] bytes) {
return DecodeUtils.decode(bytes);
}
static byte[] encode(String string) {
return DecodeUtils.encode(string);
}
static String[] decodeMultiple(byte[]... bytes) {
return DecodeUtils.decodeMultiple(bytes);
}
static Map<byte[], byte[]> encodeMap(Map<String, byte[]> map) {
return DecodeUtils.encodeMap(map);
}
static Map<String, byte[]> decodeMap(Map<byte[], byte[]> tuple) {
return DecodeUtils.decodeMap(tuple);
}
static Set<byte[]> convertToSet(Collection<String> keys) {
return DecodeUtils.convertToSet(keys);
}
static Sort applySortingParams(Sort jredisSort, SortParameters params, byte[] storeKey) {
if (params != null) {
byte[] byPattern = params.getByPattern();
if (byPattern != null) {
jredisSort.BY(decode(byPattern));
jredisSort.BY(byPattern);
}
byte[][] getPattern = params.getGetPattern();
if (getPattern != null && getPattern.length > 0) {
for (byte[] bs : getPattern) {
jredisSort.GET(decode(bs));
jredisSort.GET(bs);
}
}
Range limit = params.getLimit();
@@ -132,7 +111,7 @@ public abstract class JredisUtils {
}
if (storeKey != null) {
jredisSort.STORE(decode(storeKey));
jredisSort.STORE(storeKey);
}