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

@@ -32,7 +32,7 @@ dependencies {
compile "org.slf4j:jcl-over-slf4j:$slf4jVersion"
testRuntime "log4j:log4j:$log4jVersion"
testRuntime "org.slf4j:slf4j-log4j12:$slf4jVersion"
// Spring Framework
compile("org.springframework:spring-core:$springVersion") {
exclude module: "commons-logging"
@@ -44,9 +44,10 @@ dependencies {
// Redis Drivers
compile "redis.clients:jedis:$jedisVersion"
compile("com.github.spullara.redis:client:$srpVersion", optional)
compile("org.jredis:jredis-anthonylauzon:$jredisVersion", optional)
compile("org.jredis:jredis-core-api:$jredisVersion", optional)
compile("org.jredis:jredis-core-ri:$jredisVersion", optional)
compile("com.lambdaworks:lettuce:$lettuceVersion", optional)
// Mappers
compile("org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion", optional)
compile("commons-beanutils:commons-beanutils-core:1.8.3", optional)
@@ -60,13 +61,13 @@ dependencies {
}
sourceCompatibility = 1.5
targetCompatibility = 1.5
targetCompatibility = 1.5
javadoc {
ext.srcDir = file("${projectDir}/docs/src/api")
destinationDir = file("${buildDir}/api")
ext.tmpDir = file("${buildDir}/api-work")
configure(options) {
stylesheetFile = file("${srcDir}/spring-javadoc.css")
overview = "${srcDir}/overview.html"
@@ -77,16 +78,16 @@ javadoc {
groups = [
'Spring Data Redis Support' : ['org.springframework.data.redis*'],
]
links = [
"http://static.springframework.org/spring/docs/3.1.x/javadoc-api",
"http://download.oracle.com/javase/6/docs/api",
"http://jackson.codehaus.org/1.8.2/javadoc"
]
exclude "org/springframework/data/redis/config/**"
}
title = "${rootProject.description} ${version} API"
}
@@ -149,7 +150,7 @@ task schemaZip(type: Zip) {
description = "Builds -${classifier} archive containing all XSDs for deployment"
def Properties schemas = new Properties();
sourceSets.main.resources.find {
it.path.endsWith('META-INF' + File.separator + 'spring.schemas')
}?.withInputStream { schemas.load(it) }
@@ -162,7 +163,7 @@ task schemaZip(type: Zip) {
it.path.replace('\\', '/').endsWith(schemas.get(key))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
rename { String fileName -> alias }

View File

@@ -120,8 +120,6 @@
<para><ulink url="http://github.com/alphazero/jredis">JRedis</ulink> is another popular, open-source connector supported by Spring Data Redis through the
<literal>org.springframework.data.redis.connection.jredis</literal> package.</para>
<note>Since JRedis itself did not support Redis 2.x commands at the time of SDR's first release, Spring Data Redis uses a fork available
<ulink url="http://github.com/anthonylauzon/jredis">here</ulink>.</note>
<para>A typical JRedis configuration can looks like this:</para>
@@ -138,13 +136,6 @@
<para>As one can note, the configuration is quite similar to the Jedis one.</para>
<important><para>Currently, JRedis does not have support for binary keys. This forces the <classname>JredisConnection</classname> to perform encoding internally
(through <ulink url="http://en.wikipedia.org/wiki/Base64">base64</ulink> schema). In practice, this means it's safe to read/write arbitrary data however
the Redis key stored values will differ from the decoded ones, even in the simplest cases, since everything (no matter the format) is encoded. This will not be
the case for Redis values.</para>
<para>This issue is currently being addressed in the JRedis project and once fixed, will be incorporated by Spring Data Redis.</para>
</important>
</section>
<section id="redis:connectors:srp">

View File

@@ -1,6 +1,6 @@
slf4jVersion=1.6.6
junitVersion=4.8.1
jredisVersion=03122010
jredisVersion=06052013
jedisVersion=2.1.0
springVersion=3.1.4.RELEASE
log4jVersion=1.2.17

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

View File

@@ -206,7 +206,6 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
public void testNullKey() throws Exception {
connection.decr(EMPTY_ARRAY);
try {
connection.decr((String) null);
fail("Decrement should fail with null key");
@@ -230,7 +229,6 @@ public abstract class AbstractConnectionIntegrationTests {
@Test
public void testHashNullKey() throws Exception {
byte[] key = UUID.randomUUID().toString().getBytes();
connection.hExists(key, EMPTY_ARRAY);
try {
connection.hExists(key, null);
fail("hExists should fail with null key");

View File

@@ -17,19 +17,19 @@
package org.springframework.data.redis.connection.jredis;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays;
import org.jredis.protocol.BulkResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests;
import org.springframework.data.redis.connection.DefaultSortParameters;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.SortParameters.Order;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -45,13 +45,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration
public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrationTests {
@Before
public void setUp() {
byteConnection = connectionFactory.getConnection();
connection = new DefaultStringRedisConnection(byteConnection);
}
@After
public void tearDown() {
try {
@@ -66,22 +59,6 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
connection = null;
}
@Ignore("nulls are encoded to empty strings")
public void testNullKey() throws Exception {
}
@Ignore("nulls are encoded to empty strings")
public void testNullValue() throws Exception {
}
@Ignore("nulls are encoded to empty strings")
public void testHashNullKey() throws Exception {
}
@Ignore("nulls are encoded to empty strings")
public void testHashNullValue() throws Exception {
}
@Ignore("Pub/Sub not supported")
public void testPubSubWithPatterns() {
}
@@ -90,17 +67,22 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
public void testPubSubWithNamedChannels() {
}
@Ignore("DATAREDIS-129 Key search does not work with regex")
public void testKeys() throws Exception {
@Ignore("https://github.com/alphazero/jredis/issues/64 Protocol error: expected '$' got '*' on mset")
public void testMSet() {
}
@Ignore("DATAREDIS-171 JRedis StringIndexOutOfBoundsException in info() method in Redis 2.6")
public void testInfo() throws Exception {
@Ignore("https://github.com/alphazero/jredis/issues/64 Protocol error: expected '$' got '*' on mset")
public void testMSetNx() {
}
@Test(expected = UnsupportedOperationException.class)
public void testBitSet() throws Exception {
super.testBitSet();
@Test
public void testConnectionClosesWhenNotPooled() {
connection.close();
try {
connection.ping();
fail("Expected RedisConnectionFailureException trying to use a closed connection");
} catch (RedisConnectionFailureException e) {
}
}
@Test(expected = UnsupportedOperationException.class)
@@ -273,7 +255,6 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
super.testZRevRangeByScoreWithScoresOffsetCount();
}
// Jredis returns null for rPush
@Test
public void testSort() {
@@ -289,10 +270,9 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
connection.rPush("sortlist", "foo");
connection.rPush("sortlist", "bar");
connection.rPush("sortlist", "baz");
assertEquals(Long.valueOf(3), connection.sort("sortlist", new DefaultSortParameters(null,
Order.ASC, true), "newlist"));
assertEquals(Arrays.asList(new String[] { "bar", "baz", "foo" }),
connection.lRange("newlist", 0, 9));
assertEquals(Long.valueOf(3),
connection.sort("sortlist", new DefaultSortParameters(null, Order.ASC, true), "newlist"));
assertEquals(Arrays.asList(new String[] { "bar", "baz", "foo" }), connection.lRange("newlist", 0, 9));
}
@Test
@@ -309,8 +289,7 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
connection.rPush("PopList", "world");
connection.rPush("PopList", "hello");
assertEquals(Long.valueOf(2), connection.lRem("PopList", 2, "hello"));
assertEquals(Arrays.asList(new String[] { "big", "world" }),
connection.lRange("PopList", 0, -1));
assertEquals(Arrays.asList(new String[] { "big", "world" }), connection.lRange("PopList", 0, -1));
}
@Test
@@ -329,8 +308,7 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
connection.rPush("PopList", "big");
connection.rPush("PopList", "world");
connection.lTrim("PopList", 1, -1);
assertEquals(Arrays.asList(new String[] { "big", "world" }),
connection.lRange("PopList", 0, -1));
assertEquals(Arrays.asList(new String[] { "big", "world" }), connection.lRange("PopList", 0, -1));
}
@Test
@@ -347,8 +325,7 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
connection.rPush("pop2", "hey");
assertEquals("world", connection.rPopLPush("PopList", "pop2"));
assertEquals(Arrays.asList(new String[] { "hello" }), connection.lRange("PopList", 0, -1));
assertEquals(Arrays.asList(new String[] { "world", "hey" }),
connection.lRange("pop2", 0, -1));
assertEquals(Arrays.asList(new String[] { "world", "hey" }), connection.lRange("pop2", 0, -1));
}
@Test
@@ -361,14 +338,13 @@ public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrat
public void testLPush() throws Exception {
connection.lPush("testlist", "bar");
connection.lPush("testlist", "baz");
assertEquals(Arrays.asList(new String[] { "baz", "bar" }),
connection.lRange("testlist", 0, -1));
assertEquals(Arrays.asList(new String[] { "baz", "bar" }), connection.lRange("testlist", 0, -1));
}
@Test
public void testExecute() {
connection.set("foo", "bar");
BulkResponse response = (BulkResponse) connection.execute("GET", JredisUtils.decode("foo".getBytes()));
BulkResponse response = (BulkResponse) connection.execute("GET", "foo".getBytes());
assertEquals("bar", stringSerializer.deserialize(response.getBulkData()));
}

View File

@@ -40,7 +40,6 @@ public abstract class AtomicCountersParam {
// JRedis
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
jredisConnFactory.setUsePool(true);
jredisConnFactory.setPort(SettingsUtils.getPort());
jredisConnFactory.setHostName(SettingsUtils.getHost());
jredisConnFactory.afterPropertiesSet();

View File

@@ -81,7 +81,6 @@ public abstract class CollectionTestParams {
// jredis
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
jredisConnFactory.setUsePool(true);
jredisConnFactory.setPort(SettingsUtils.getPort());
jredisConnFactory.setHostName(SettingsUtils.getHost());

View File

@@ -89,7 +89,6 @@ public class RedisMapTests extends AbstractRedisMapTests<Object, Object> {
// JRedis
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
jredisConnFactory.setUsePool(true);
jredisConnFactory.setPort(SettingsUtils.getPort());
jredisConnFactory.setHostName(SettingsUtils.getHost());
jredisConnFactory.afterPropertiesSet();

View File

@@ -259,7 +259,6 @@ public class RedisPropertiesTests extends RedisMapTests {
// JRedis
JredisConnectionFactory jredisConnFactory = new JredisConnectionFactory();
jredisConnFactory.setUsePool(true);
jredisConnFactory.setPort(SettingsUtils.getPort());
jredisConnFactory.setHostName(SettingsUtils.getHost());
jredisConnFactory.afterPropertiesSet();

View File

@@ -3,12 +3,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DATAREDIS-154 We can't pool connections for this test because JRedis
closes connections on Exception but doesn't remove them from the pool or
reconnect them on next attempted use -->
<bean id="jredisConnectionFactory "
class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory"
p:usePool="false">
class="org.springframework.data.redis.connection.jredis.JredisConnectionFactory">
<property name="hostName">
<bean class="org.springframework.data.redis.SettingsUtils"
factory-method="getHost" />