Add a pool for sharded jedis

This commit is contained in:
Jonathan Leibiusky
2010-11-10 13:41:41 -03:00
parent 08f8432215
commit f62548931c
6 changed files with 382 additions and 184 deletions

View File

@@ -50,13 +50,6 @@ public class Connection {
} }
protected Connection sendCommand(String name, String... args) { protected Connection sendCommand(String name, String... args) {
try {
connect();
} catch (UnknownHostException e) {
throw new JedisException("Could not connect to redis-server", e);
} catch (IOException e) {
throw new JedisException("Could not connect to redis-server", e);
}
protocol.sendCommand(outputStream, name, args); protocol.sendCommand(outputStream, name, args);
pipelinedCommands++; pipelinedCommands++;
return this; return this;

View File

@@ -14,6 +14,7 @@ import redis.clients.jedis.Client.LIST_POSITION;
public class Jedis implements JedisCommands { public class Jedis implements JedisCommands {
private Client client = null; private Client client = null;
private String password = null;
public Jedis(String host) { public Jedis(String host) {
client = new Client(host); client = new Client(host);
@@ -31,13 +32,11 @@ public class Jedis implements JedisCommands {
public Jedis(JedisShardInfo shardInfo) { public Jedis(JedisShardInfo shardInfo) {
client = new Client(shardInfo.getHost(), shardInfo.getPort()); client = new Client(shardInfo.getHost(), shardInfo.getPort());
client.setTimeout(shardInfo.getTimeout()); client.setTimeout(shardInfo.getTimeout());
if (shardInfo.getPassword() != null) { this.password = shardInfo.getPassword();
this.auth(shardInfo.getPassword());
}
} }
public String ping() { public String ping() {
checkIsInMulti(); runChecks();
client.ping(); client.ping();
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -53,7 +52,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String set(String key, String value) { public String set(String key, String value) {
checkIsInMulti(); runChecks();
client.set(key, value); client.set(key, value);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -69,7 +68,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String get(String key) { public String get(String key) {
checkIsInMulti(); runChecks();
client.sendCommand("GET", key); client.sendCommand("GET", key);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -78,7 +77,7 @@ public class Jedis implements JedisCommands {
* Ask the server to silently close the connection. * Ask the server to silently close the connection.
*/ */
public void quit() { public void quit() {
checkIsInMulti(); runChecks();
client.quit(); client.quit();
} }
@@ -93,7 +92,7 @@ public class Jedis implements JedisCommands {
* @return Integer reply, "0" if the key exists, otherwise "1" * @return Integer reply, "0" if the key exists, otherwise "1"
*/ */
public Integer exists(String key) { public Integer exists(String key) {
checkIsInMulti(); runChecks();
client.exists(key); client.exists(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -109,7 +108,7 @@ public class Jedis implements JedisCommands {
* more keys were removed 0 if none of the specified key existed * more keys were removed 0 if none of the specified key existed
*/ */
public Integer del(String... keys) { public Integer del(String... keys) {
checkIsInMulti(); runChecks();
client.del(keys); client.del(keys);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -129,7 +128,7 @@ public class Jedis implements JedisCommands {
* contains a Hash value * contains a Hash value
*/ */
public String type(String key) { public String type(String key) {
checkIsInMulti(); runChecks();
client.type(key); client.type(key);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -141,7 +140,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String flushDB() { public String flushDB() {
checkIsInMulti(); runChecks();
client.flushDB(); client.flushDB();
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -178,7 +177,7 @@ public class Jedis implements JedisCommands {
* @return Multi bulk reply * @return Multi bulk reply
*/ */
public List<String> keys(String pattern) { public List<String> keys(String pattern) {
checkIsInMulti(); runChecks();
client.keys(pattern); client.keys(pattern);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -192,7 +191,7 @@ public class Jedis implements JedisCommands {
* empty string is the database is empty * empty string is the database is empty
*/ */
public String randomKey() { public String randomKey() {
checkIsInMulti(); runChecks();
client.randomKey(); client.randomKey();
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -209,7 +208,7 @@ public class Jedis implements JedisCommands {
* @return Status code repy * @return Status code repy
*/ */
public String rename(String oldkey, String newkey) { public String rename(String oldkey, String newkey) {
checkIsInMulti(); runChecks();
client.rename(oldkey, newkey); client.rename(oldkey, newkey);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -226,7 +225,7 @@ public class Jedis implements JedisCommands {
* target key already exist * target key already exist
*/ */
public Integer renamenx(String oldkey, String newkey) { public Integer renamenx(String oldkey, String newkey) {
checkIsInMulti(); runChecks();
client.renamenx(oldkey, newkey); client.renamenx(oldkey, newkey);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -237,7 +236,7 @@ public class Jedis implements JedisCommands {
* @return Integer reply * @return Integer reply
*/ */
public Integer dbSize() { public Integer dbSize() {
checkIsInMulti(); runChecks();
client.dbSize(); client.dbSize();
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -271,7 +270,7 @@ public class Jedis implements JedisCommands {
* exist. * exist.
*/ */
public Integer expire(String key, int seconds) { public Integer expire(String key, int seconds) {
checkIsInMulti(); runChecks();
client.expire(key, seconds); client.expire(key, seconds);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -307,7 +306,7 @@ public class Jedis implements JedisCommands {
* exist. * exist.
*/ */
public Integer expireAt(String key, long unixTime) { public Integer expireAt(String key, long unixTime) {
checkIsInMulti(); runChecks();
client.expireAt(key, unixTime); client.expireAt(key, unixTime);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -324,7 +323,7 @@ public class Jedis implements JedisCommands {
* have an associated expire, -1 is returned. * have an associated expire, -1 is returned.
*/ */
public Integer ttl(String key) { public Integer ttl(String key) {
checkIsInMulti(); runChecks();
client.ttl(key); client.ttl(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -337,7 +336,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String select(int index) { public String select(int index) {
checkIsInMulti(); runChecks();
client.select(index); client.select(index);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -356,7 +355,7 @@ public class Jedis implements JedisCommands {
* found in the current DB. * found in the current DB.
*/ */
public Integer move(String key, int dbIndex) { public Integer move(String key, int dbIndex) {
checkIsInMulti(); runChecks();
client.move(key, dbIndex); client.move(key, dbIndex);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -368,7 +367,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String flushAll() { public String flushAll() {
checkIsInMulti(); runChecks();
client.flushAll(); client.flushAll();
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -385,7 +384,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String getSet(String key, String value) { public String getSet(String key, String value) {
checkIsInMulti(); runChecks();
client.getSet(key, value); client.getSet(key, value);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -401,7 +400,7 @@ public class Jedis implements JedisCommands {
* @return Multi bulk reply * @return Multi bulk reply
*/ */
public List<String> mget(String... keys) { public List<String> mget(String... keys) {
checkIsInMulti(); runChecks();
client.mget(keys); client.mget(keys);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -419,7 +418,7 @@ public class Jedis implements JedisCommands {
* was not set * was not set
*/ */
public Integer setnx(String key, String value) { public Integer setnx(String key, String value) {
checkIsInMulti(); runChecks();
client.setnx(key, value); client.setnx(key, value);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -437,7 +436,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String setex(String key, int seconds, String value) { public String setex(String key, int seconds, String value) {
checkIsInMulti(); runChecks();
client.setex(key, seconds, value); client.setex(key, seconds, value);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -463,7 +462,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply Basically +OK as MSET can't fail * @return Status code reply Basically +OK as MSET can't fail
*/ */
public String mset(String... keysvalues) { public String mset(String... keysvalues) {
checkIsInMulti(); runChecks();
client.mset(keysvalues); client.mset(keysvalues);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -490,7 +489,7 @@ public class Jedis implements JedisCommands {
* no key was set (at least one key already existed) * no key was set (at least one key already existed)
*/ */
public Integer msetnx(String... keysvalues) { public Integer msetnx(String... keysvalues) {
checkIsInMulti(); runChecks();
client.msetnx(keysvalues); client.msetnx(keysvalues);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -518,7 +517,7 @@ public class Jedis implements JedisCommands {
* after the increment. * after the increment.
*/ */
public Integer decrBy(String key, int integer) { public Integer decrBy(String key, int integer) {
checkIsInMulti(); runChecks();
client.decrBy(key, integer); client.decrBy(key, integer);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -546,7 +545,7 @@ public class Jedis implements JedisCommands {
* after the increment. * after the increment.
*/ */
public Integer decr(String key) { public Integer decr(String key) {
checkIsInMulti(); runChecks();
client.decr(key); client.decr(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -574,7 +573,7 @@ public class Jedis implements JedisCommands {
* after the increment. * after the increment.
*/ */
public Integer incrBy(String key, int integer) { public Integer incrBy(String key, int integer) {
checkIsInMulti(); runChecks();
client.incrBy(key, integer); client.incrBy(key, integer);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -602,7 +601,7 @@ public class Jedis implements JedisCommands {
* after the increment. * after the increment.
*/ */
public Integer incr(String key) { public Integer incr(String key) {
checkIsInMulti(); runChecks();
client.incr(key); client.incr(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -624,7 +623,7 @@ public class Jedis implements JedisCommands {
* the append operation. * the append operation.
*/ */
public Integer append(String key, String value) { public Integer append(String key, String value) {
checkIsInMulti(); runChecks();
client.append(key, value); client.append(key, value);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -648,7 +647,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String substr(String key, int start, int end) { public String substr(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.substr(key, start, end); client.substr(key, start, end);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -669,7 +668,7 @@ public class Jedis implements JedisCommands {
* 1 is returned. * 1 is returned.
*/ */
public Integer hset(String key, String field, String value) { public Integer hset(String key, String field, String value) {
checkIsInMulti(); runChecks();
client.hset(key, field, value); client.hset(key, field, value);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -688,7 +687,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String hget(String key, String field) { public String hget(String key, String field) {
checkIsInMulti(); runChecks();
client.hget(key, field); client.hget(key, field);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -705,7 +704,7 @@ public class Jedis implements JedisCommands {
* field is created 1 is returned. * field is created 1 is returned.
*/ */
public Integer hsetnx(String key, String field, String value) { public Integer hsetnx(String key, String field, String value) {
checkIsInMulti(); runChecks();
client.hsetnx(key, field, value); client.hsetnx(key, field, value);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -723,7 +722,7 @@ public class Jedis implements JedisCommands {
* @return Always OK because HMSET can't fail * @return Always OK because HMSET can't fail
*/ */
public String hmset(String key, Map<String, String> hash) { public String hmset(String key, Map<String, String> hash) {
checkIsInMulti(); runChecks();
client.hmset(key, hash); client.hmset(key, hash);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -742,7 +741,7 @@ public class Jedis implements JedisCommands {
* with the specified fields, in the same order of the request. * with the specified fields, in the same order of the request.
*/ */
public List<String> hmget(String key, String... fields) { public List<String> hmget(String key, String... fields) {
checkIsInMulti(); runChecks();
client.hmget(key, fields); client.hmget(key, fields);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -766,7 +765,7 @@ public class Jedis implements JedisCommands {
* operation. * operation.
*/ */
public Integer hincrBy(String key, String field, int value) { public Integer hincrBy(String key, String field, int value) {
checkIsInMulti(); runChecks();
client.hincrBy(key, field, value); client.hincrBy(key, field, value);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -782,7 +781,7 @@ public class Jedis implements JedisCommands {
* Return 0 if the key is not found or the field is not present. * Return 0 if the key is not found or the field is not present.
*/ */
public Integer hexists(String key, String field) { public Integer hexists(String key, String field) {
checkIsInMulti(); runChecks();
client.hexists(key, field); client.hexists(key, field);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -798,7 +797,7 @@ public class Jedis implements JedisCommands {
* returned, otherwise 0 is returned and no operation is performed. * returned, otherwise 0 is returned and no operation is performed.
*/ */
public Integer hdel(String key, String field) { public Integer hdel(String key, String field) {
checkIsInMulti(); runChecks();
client.hdel(key, field); client.hdel(key, field);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -814,7 +813,7 @@ public class Jedis implements JedisCommands {
* an empty hash. * an empty hash.
*/ */
public Integer hlen(String key) { public Integer hlen(String key) {
checkIsInMulti(); runChecks();
client.hlen(key); client.hlen(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -828,7 +827,7 @@ public class Jedis implements JedisCommands {
* @return All the fields names contained into a hash. * @return All the fields names contained into a hash.
*/ */
public List<String> hkeys(String key) { public List<String> hkeys(String key) {
checkIsInMulti(); runChecks();
client.hkeys(key); client.hkeys(key);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -842,7 +841,7 @@ public class Jedis implements JedisCommands {
* @return All the fields values contained into a hash. * @return All the fields values contained into a hash.
*/ */
public List<String> hvals(String key) { public List<String> hvals(String key) {
checkIsInMulti(); runChecks();
client.hvals(key); client.hvals(key);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -856,7 +855,7 @@ public class Jedis implements JedisCommands {
* @return All the fields and values contained into a hash. * @return All the fields and values contained into a hash.
*/ */
public Map<String, String> hgetAll(String key) { public Map<String, String> hgetAll(String key) {
checkIsInMulti(); runChecks();
client.hgetAll(key); client.hgetAll(key);
List<String> flatHash = client.getMultiBulkReply(); List<String> flatHash = client.getMultiBulkReply();
Map<String, String> hash = new HashMap<String, String>(); Map<String, String> hash = new HashMap<String, String>();
@@ -884,7 +883,7 @@ public class Jedis implements JedisCommands {
* list after the push operation. * list after the push operation.
*/ */
public Integer rpush(String key, String string) { public Integer rpush(String key, String string) {
checkIsInMulti(); runChecks();
client.rpush(key, string); client.rpush(key, string);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -905,7 +904,7 @@ public class Jedis implements JedisCommands {
* list after the push operation. * list after the push operation.
*/ */
public Integer lpush(String key, String string) { public Integer lpush(String key, String string) {
checkIsInMulti(); runChecks();
client.lpush(key, string); client.lpush(key, string);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -921,7 +920,7 @@ public class Jedis implements JedisCommands {
* @return The length of the list. * @return The length of the list.
*/ */
public Integer llen(String key) { public Integer llen(String key) {
checkIsInMulti(); runChecks();
client.llen(key); client.llen(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -965,7 +964,7 @@ public class Jedis implements JedisCommands {
* specified range. * specified range.
*/ */
public List<String> lrange(String key, int start, int end) { public List<String> lrange(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.lrange(key, start, end); client.lrange(key, start, end);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -1005,7 +1004,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String ltrim(String key, int start, int end) { public String ltrim(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.ltrim(key, start, end); client.ltrim(key, start, end);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -1029,7 +1028,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply, specifically the requested element * @return Bulk reply, specifically the requested element
*/ */
public String lindex(String key, int index) { public String lindex(String key, int index) {
checkIsInMulti(); runChecks();
client.lindex(key, index); client.lindex(key, index);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -1056,7 +1055,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String lset(String key, int index, String value) { public String lset(String key, int index, String value) {
checkIsInMulti(); runChecks();
client.lset(key, index, value); client.lset(key, index, value);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -1081,7 +1080,7 @@ public class Jedis implements JedisCommands {
* the operation succeeded * the operation succeeded
*/ */
public Integer lrem(String key, int count, String value) { public Integer lrem(String key, int count, String value) {
checkIsInMulti(); runChecks();
client.lrem(key, count, value); client.lrem(key, count, value);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1100,7 +1099,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String lpop(String key) { public String lpop(String key) {
checkIsInMulti(); runChecks();
client.lpop(key); client.lpop(key);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -1119,7 +1118,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String rpop(String key) { public String rpop(String key) {
checkIsInMulti(); runChecks();
client.rpop(key); client.rpop(key);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -1143,7 +1142,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String rpoplpush(String srckey, String dstkey) { public String rpoplpush(String srckey, String dstkey) {
checkIsInMulti(); runChecks();
client.rpoplpush(srckey, dstkey); client.rpoplpush(srckey, dstkey);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -1162,7 +1161,7 @@ public class Jedis implements JedisCommands {
* the element was already a member of the set * the element was already a member of the set
*/ */
public Integer sadd(String key, String member) { public Integer sadd(String key, String member) {
checkIsInMulti(); runChecks();
client.sadd(key, member); client.sadd(key, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1177,7 +1176,7 @@ public class Jedis implements JedisCommands {
* @return Multi bulk reply * @return Multi bulk reply
*/ */
public Set<String> smembers(String key) { public Set<String> smembers(String key) {
checkIsInMulti(); runChecks();
client.smembers(key); client.smembers(key);
List<String> members = client.getMultiBulkReply(); List<String> members = client.getMultiBulkReply();
return new LinkedHashSet<String>(members); return new LinkedHashSet<String>(members);
@@ -1196,7 +1195,7 @@ public class Jedis implements JedisCommands {
* if the new element was not a member of the set * if the new element was not a member of the set
*/ */
public Integer srem(String key, String member) { public Integer srem(String key, String member) {
checkIsInMulti(); runChecks();
client.srem(key, member); client.srem(key, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1214,7 +1213,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String spop(String key) { public String spop(String key) {
checkIsInMulti(); runChecks();
client.spop(key); client.spop(key);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -1243,7 +1242,7 @@ public class Jedis implements JedisCommands {
* performed * performed
*/ */
public Integer smove(String srckey, String dstkey, String member) { public Integer smove(String srckey, String dstkey, String member) {
checkIsInMulti(); runChecks();
client.smove(srckey, dstkey, member); client.smove(srckey, dstkey, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1257,7 +1256,7 @@ public class Jedis implements JedisCommands {
* of the set as an integer. * of the set as an integer.
*/ */
public Integer scard(String key) { public Integer scard(String key) {
checkIsInMulti(); runChecks();
client.scard(key); client.scard(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1275,7 +1274,7 @@ public class Jedis implements JedisCommands {
* does not exist * does not exist
*/ */
public Integer sismember(String key, String member) { public Integer sismember(String key, String member) {
checkIsInMulti(); runChecks();
client.sismember(key, member); client.sismember(key, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1300,7 +1299,7 @@ public class Jedis implements JedisCommands {
* @return Multi bulk reply, specifically the list of common elements. * @return Multi bulk reply, specifically the list of common elements.
*/ */
public Set<String> sinter(String... keys) { public Set<String> sinter(String... keys) {
checkIsInMulti(); runChecks();
client.sinter(keys); client.sinter(keys);
List<String> members = client.getMultiBulkReply(); List<String> members = client.getMultiBulkReply();
return new LinkedHashSet<String>(members); return new LinkedHashSet<String>(members);
@@ -1318,7 +1317,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public Integer sinterstore(String dstkey, String... keys) { public Integer sinterstore(String dstkey, String... keys) {
checkIsInMulti(); runChecks();
client.sinterstore(dstkey, keys); client.sinterstore(dstkey, keys);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1340,7 +1339,7 @@ public class Jedis implements JedisCommands {
* @return Multi bulk reply, specifically the list of common elements. * @return Multi bulk reply, specifically the list of common elements.
*/ */
public Set<String> sunion(String... keys) { public Set<String> sunion(String... keys) {
checkIsInMulti(); runChecks();
client.sunion(keys); client.sunion(keys);
List<String> members = client.getMultiBulkReply(); List<String> members = client.getMultiBulkReply();
return new LinkedHashSet<String>(members); return new LinkedHashSet<String>(members);
@@ -1359,7 +1358,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public Integer sunionstore(String dstkey, String... keys) { public Integer sunionstore(String dstkey, String... keys) {
checkIsInMulti(); runChecks();
client.sunionstore(dstkey, keys); client.sunionstore(dstkey, keys);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1388,7 +1387,7 @@ public class Jedis implements JedisCommands {
* the first set provided and all the successive sets. * the first set provided and all the successive sets.
*/ */
public Set<String> sdiff(String... keys) { public Set<String> sdiff(String... keys) {
checkIsInMulti(); runChecks();
client.sdiff(keys); client.sdiff(keys);
List<String> members = client.getMultiBulkReply(); List<String> members = client.getMultiBulkReply();
return new LinkedHashSet<String>(members); return new LinkedHashSet<String>(members);
@@ -1403,7 +1402,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public Integer sdiffstore(String dstkey, String... keys) { public Integer sdiffstore(String dstkey, String... keys) {
checkIsInMulti(); runChecks();
client.sdiffstore(dstkey, keys); client.sdiffstore(dstkey, keys);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1421,7 +1420,7 @@ public class Jedis implements JedisCommands {
* @return Bulk reply * @return Bulk reply
*/ */
public String srandmember(String key) { public String srandmember(String key) {
checkIsInMulti(); runChecks();
client.srandmember(key); client.srandmember(key);
return client.getBulkReply(); return client.getBulkReply();
} }
@@ -1448,13 +1447,13 @@ public class Jedis implements JedisCommands {
* was updated * was updated
*/ */
public Integer zadd(String key, double score, String member) { public Integer zadd(String key, double score, String member) {
checkIsInMulti(); runChecks();
client.zadd(key, score, member); client.zadd(key, score, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
public Set<String> zrange(String key, int start, int end) { public Set<String> zrange(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.zrange(key, start, end); client.zrange(key, start, end);
List<String> members = client.getMultiBulkReply(); List<String> members = client.getMultiBulkReply();
return new LinkedHashSet<String>(members); return new LinkedHashSet<String>(members);
@@ -1476,7 +1475,7 @@ public class Jedis implements JedisCommands {
* if the new element was not a member of the set * if the new element was not a member of the set
*/ */
public Integer zrem(String key, String member) { public Integer zrem(String key, String member) {
checkIsInMulti(); runChecks();
client.zrem(key, member); client.zrem(key, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1506,7 +1505,7 @@ public class Jedis implements JedisCommands {
* @return The new score * @return The new score
*/ */
public Double zincrby(String key, double score, String member) { public Double zincrby(String key, double score, String member) {
checkIsInMulti(); runChecks();
client.zincrby(key, score, member); client.zincrby(key, score, member);
String newscore = client.getBulkReply(); String newscore = client.getBulkReply();
return Double.valueOf(newscore); return Double.valueOf(newscore);
@@ -1533,7 +1532,7 @@ public class Jedis implements JedisCommands {
* reply if there is no such element. * reply if there is no such element.
*/ */
public Integer zrank(String key, String member) { public Integer zrank(String key, String member) {
checkIsInMulti(); runChecks();
client.zrank(key, member); client.zrank(key, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1559,27 +1558,27 @@ public class Jedis implements JedisCommands {
* reply if there is no such element. * reply if there is no such element.
*/ */
public Integer zrevrank(String key, String member) { public Integer zrevrank(String key, String member) {
checkIsInMulti(); runChecks();
client.zrevrank(key, member); client.zrevrank(key, member);
return client.getIntegerReply(); return client.getIntegerReply();
} }
public Set<String> zrevrange(String key, int start, int end) { public Set<String> zrevrange(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.zrevrange(key, start, end); client.zrevrange(key, start, end);
List<String> members = client.getMultiBulkReply(); List<String> members = client.getMultiBulkReply();
return new LinkedHashSet<String>(members); return new LinkedHashSet<String>(members);
} }
public Set<Tuple> zrangeWithScores(String key, int start, int end) { public Set<Tuple> zrangeWithScores(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.zrangeWithScores(key, start, end); client.zrangeWithScores(key, start, end);
Set<Tuple> set = getTupledSet(); Set<Tuple> set = getTupledSet();
return set; return set;
} }
public Set<Tuple> zrevrangeWithScores(String key, int start, int end) { public Set<Tuple> zrevrangeWithScores(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.zrevrangeWithScores(key, start, end); client.zrevrangeWithScores(key, start, end);
Set<Tuple> set = getTupledSet(); Set<Tuple> set = getTupledSet();
return set; return set;
@@ -1595,7 +1594,7 @@ public class Jedis implements JedisCommands {
* @return the cardinality (number of elements) of the set as an integer. * @return the cardinality (number of elements) of the set as an integer.
*/ */
public Integer zcard(String key) { public Integer zcard(String key) {
checkIsInMulti(); runChecks();
client.zcard(key); client.zcard(key);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1612,7 +1611,7 @@ public class Jedis implements JedisCommands {
* @return the score * @return the score
*/ */
public Double zscore(String key, String member) { public Double zscore(String key, String member) {
checkIsInMulti(); runChecks();
client.zscore(key, member); client.zscore(key, member);
String score = client.getBulkReply(); String score = client.getBulkReply();
return (score != null ? new Double(score) : null); return (score != null ? new Double(score) : null);
@@ -1637,15 +1636,27 @@ public class Jedis implements JedisCommands {
return results; return results;
} }
private void checkIsInMulti() { private void runChecks() {
if (client.isInMulti()) { if (client.isInMulti()) {
throw new JedisException( throw new JedisException(
"Cannot use Jedis when in Multi. Please use JedisTransaction instead."); "Cannot use Jedis when in Multi. Please use JedisTransaction instead.");
} }
try {
this.connect();
} catch (UnknownHostException e) {
throw new JedisException(e);
} catch (IOException e) {
throw new JedisException(e);
}
} }
public void connect() throws UnknownHostException, IOException { public void connect() throws UnknownHostException, IOException {
client.connect(); if (!client.isConnected()) {
client.connect();
if (this.password != null) {
this.auth(this.password);
}
}
} }
public void disconnect() throws IOException { public void disconnect() throws IOException {
@@ -1680,7 +1691,7 @@ public class Jedis implements JedisCommands {
* smallest to the biggest number. * smallest to the biggest number.
*/ */
public List<String> sort(String key) { public List<String> sort(String key) {
checkIsInMulti(); runChecks();
client.sort(key); client.sort(key);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -1762,7 +1773,7 @@ public class Jedis implements JedisCommands {
* @return a list of sorted elements. * @return a list of sorted elements.
*/ */
public List<String> sort(String key, SortingParams sortingParameters) { public List<String> sort(String key, SortingParams sortingParameters) {
checkIsInMulti(); runChecks();
client.sort(key, sortingParameters); client.sort(key, sortingParameters);
return client.getMultiBulkReply(); return client.getMultiBulkReply();
} }
@@ -1840,7 +1851,7 @@ public class Jedis implements JedisCommands {
* programming language used. * programming language used.
*/ */
public List<String> blpop(int timeout, String... keys) { public List<String> blpop(int timeout, String... keys) {
checkIsInMulti(); runChecks();
List<String> args = new ArrayList<String>(); List<String> args = new ArrayList<String>();
for (String arg : keys) { for (String arg : keys) {
args.add(arg); args.add(arg);
@@ -1869,7 +1880,7 @@ public class Jedis implements JedisCommands {
*/ */
public Integer sort(String key, SortingParams sortingParameters, public Integer sort(String key, SortingParams sortingParameters,
String dstkey) { String dstkey) {
checkIsInMulti(); runChecks();
client.sort(key, sortingParameters, dstkey); client.sort(key, sortingParameters, dstkey);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1891,7 +1902,7 @@ public class Jedis implements JedisCommands {
* @return The number of elements of the list at dstkey. * @return The number of elements of the list at dstkey.
*/ */
public Integer sort(String key, String dstkey) { public Integer sort(String key, String dstkey) {
checkIsInMulti(); runChecks();
client.sort(key, dstkey); client.sort(key, dstkey);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -1969,7 +1980,7 @@ public class Jedis implements JedisCommands {
* programming language used. * programming language used.
*/ */
public List<String> brpop(int timeout, String... keys) { public List<String> brpop(int timeout, String... keys) {
checkIsInMulti(); runChecks();
List<String> args = new ArrayList<String>(); List<String> args = new ArrayList<String>();
for (String arg : keys) { for (String arg : keys) {
args.add(arg); args.add(arg);
@@ -2000,7 +2011,7 @@ public class Jedis implements JedisCommands {
* @return Status code reply * @return Status code reply
*/ */
public String auth(String password) { public String auth(String password) {
checkIsInMulti(); runChecks();
client.auth(password); client.auth(password);
return client.getStatusCodeReply(); return client.getStatusCodeReply();
} }
@@ -2029,7 +2040,7 @@ public class Jedis implements JedisCommands {
} }
public Integer zcount(String key, double min, double max) { public Integer zcount(String key, double min, double max) {
checkIsInMulti(); runChecks();
client.zcount(key, min, max); client.zcount(key, min, max);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -2092,13 +2103,13 @@ public class Jedis implements JedisCommands {
* score range. * score range.
*/ */
public Set<String> zrangeByScore(String key, double min, double max) { public Set<String> zrangeByScore(String key, double min, double max) {
checkIsInMulti(); runChecks();
client.zrangeByScore(key, min, max); client.zrangeByScore(key, min, max);
return new LinkedHashSet<String>(client.getMultiBulkReply()); return new LinkedHashSet<String>(client.getMultiBulkReply());
} }
public Set<String> zrangeByScore(String key, String min, String max) { public Set<String> zrangeByScore(String key, String min, String max) {
checkIsInMulti(); runChecks();
client.zrangeByScore(key, min, max); client.zrangeByScore(key, min, max);
return new LinkedHashSet<String>(client.getMultiBulkReply()); return new LinkedHashSet<String>(client.getMultiBulkReply());
} }
@@ -2161,7 +2172,7 @@ public class Jedis implements JedisCommands {
*/ */
public Set<String> zrangeByScore(String key, double min, double max, public Set<String> zrangeByScore(String key, double min, double max,
int offset, int count) { int offset, int count) {
checkIsInMulti(); runChecks();
client.zrangeByScore(key, min, max, offset, count); client.zrangeByScore(key, min, max, offset, count);
return new LinkedHashSet<String>(client.getMultiBulkReply()); return new LinkedHashSet<String>(client.getMultiBulkReply());
} }
@@ -2223,7 +2234,7 @@ public class Jedis implements JedisCommands {
* score range. * score range.
*/ */
public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) { public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
checkIsInMulti(); runChecks();
client.zrangeByScoreWithScores(key, min, max); client.zrangeByScoreWithScores(key, min, max);
Set<Tuple> set = getTupledSet(); Set<Tuple> set = getTupledSet();
return set; return set;
@@ -2287,14 +2298,14 @@ public class Jedis implements JedisCommands {
*/ */
public Set<Tuple> zrangeByScoreWithScores(String key, double min, public Set<Tuple> zrangeByScoreWithScores(String key, double min,
double max, int offset, int count) { double max, int offset, int count) {
checkIsInMulti(); runChecks();
client.zrangeByScoreWithScores(key, min, max, offset, count); client.zrangeByScoreWithScores(key, min, max, offset, count);
Set<Tuple> set = getTupledSet(); Set<Tuple> set = getTupledSet();
return set; return set;
} }
private Set<Tuple> getTupledSet() { private Set<Tuple> getTupledSet() {
checkIsInMulti(); runChecks();
List<String> membersWithScores = client.getMultiBulkReply(); List<String> membersWithScores = client.getMultiBulkReply();
Set<Tuple> set = new LinkedHashSet<Tuple>(); Set<Tuple> set = new LinkedHashSet<Tuple>();
Iterator<String> iterator = membersWithScores.iterator(); Iterator<String> iterator = membersWithScores.iterator();
@@ -2320,7 +2331,7 @@ public class Jedis implements JedisCommands {
* *
*/ */
public Integer zremrangeByRank(String key, int start, int end) { public Integer zremrangeByRank(String key, int start, int end) {
checkIsInMulti(); runChecks();
client.zremrangeByRank(key, start, end); client.zremrangeByRank(key, start, end);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -2340,7 +2351,7 @@ public class Jedis implements JedisCommands {
* @return Integer reply, specifically the number of elements removed. * @return Integer reply, specifically the number of elements removed.
*/ */
public Integer zremrangeByScore(String key, double start, double end) { public Integer zremrangeByScore(String key, double start, double end) {
checkIsInMulti(); runChecks();
client.zremrangeByScore(key, start, end); client.zremrangeByScore(key, start, end);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -2384,7 +2395,7 @@ public class Jedis implements JedisCommands {
* set at dstkey * set at dstkey
*/ */
public Integer zunionstore(String dstkey, String... sets) { public Integer zunionstore(String dstkey, String... sets) {
checkIsInMulti(); runChecks();
client.zunionstore(dstkey, sets); client.zunionstore(dstkey, sets);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -2429,7 +2440,7 @@ public class Jedis implements JedisCommands {
* set at dstkey * set at dstkey
*/ */
public Integer zunionstore(String dstkey, ZParams params, String... sets) { public Integer zunionstore(String dstkey, ZParams params, String... sets) {
checkIsInMulti(); runChecks();
client.zunionstore(dstkey, params, sets); client.zunionstore(dstkey, params, sets);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -2473,7 +2484,7 @@ public class Jedis implements JedisCommands {
* set at dstkey * set at dstkey
*/ */
public Integer zinterstore(String dstkey, String... sets) { public Integer zinterstore(String dstkey, String... sets) {
checkIsInMulti(); runChecks();
client.zinterstore(dstkey, sets); client.zinterstore(dstkey, sets);
return client.getIntegerReply(); return client.getIntegerReply();
} }
@@ -2518,7 +2529,7 @@ public class Jedis implements JedisCommands {
* set at dstkey * set at dstkey
*/ */
public Integer zinterstore(String dstkey, ZParams params, String... sets) { public Integer zinterstore(String dstkey, ZParams params, String... sets) {
checkIsInMulti(); runChecks();
client.zinterstore(dstkey, params, sets); client.zinterstore(dstkey, params, sets);
return client.getIntegerReply(); return client.getIntegerReply();
} }

View File

@@ -31,14 +31,11 @@ public class ShardedJedis extends Sharded<Jedis, JedisShardInfo> implements
public void disconnect() throws IOException { public void disconnect() throws IOException {
for (JedisShardInfo jedis : getAllShards()) { for (JedisShardInfo jedis : getAllShards()) {
jedis.getResource().quit();
jedis.getResource().disconnect(); jedis.getResource().disconnect();
} }
} }
protected Jedis create(JedisShardInfo shard) {
return new Jedis(shard);
}
public String set(String key, String value) { public String set(String key, String value) {
Jedis j = getShard(key); Jedis j = getShard(key);
return j.set(key, value); return j.set(key, value);

View File

@@ -0,0 +1,82 @@
package redis.clients.jedis;
import java.util.List;
import java.util.regex.Pattern;
import redis.clients.util.FixedResourcePool;
import redis.clients.util.Hashing;
public class ShardedJedisPool extends FixedResourcePool<ShardedJedis> {
private List<JedisShardInfo> shards;
private Hashing algo = Hashing.MD5;
private Pattern keyTagPattern;
public ShardedJedisPool(List<JedisShardInfo> shards) {
this.shards = shards;
}
public ShardedJedisPool(List<JedisShardInfo> shards, Hashing algo) {
this.shards = shards;
this.algo = algo;
}
public ShardedJedisPool(List<JedisShardInfo> shards, Pattern keyTagPattern) {
this.shards = shards;
this.keyTagPattern = keyTagPattern;
}
public ShardedJedisPool(List<JedisShardInfo> shards, Hashing algo,
Pattern keyTagPattern) {
this.shards = shards;
this.algo = algo;
this.keyTagPattern = keyTagPattern;
}
@Override
protected ShardedJedis createResource() {
ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern);
boolean done = false;
while (!done) {
try {
for (JedisShardInfo shard : jedis.getAllShards()) {
if (!shard.getResource().isConnected()) {
shard.getResource().connect();
}
}
done = true;
} catch (Exception e) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
}
}
}
return jedis;
}
@Override
protected void destroyResource(ShardedJedis jedis) {
if (jedis != null) {
try {
jedis.disconnect();
} catch (Exception e) {
}
}
}
@Override
protected boolean isResourceValid(ShardedJedis jedis) {
try {
for (JedisShardInfo shard : jedis.getAllShards()) {
if (!shard.getResource().isConnected()
|| !shard.getResource().ping().equals("PONG")) {
return false;
}
}
return true;
} catch (Exception ex) {
return false;
}
}
}

View File

@@ -11,85 +11,85 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort; import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class JedisPoolTest extends Assert { public class JedisPoolTest extends Assert {
private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0);
@Test
public void checkConnections() throws TimeoutException {
JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000);
pool.setResourcesNumber(10);
pool.init();
Jedis jedis = pool.getResource(200); @Test
jedis.auth("foobared"); public void checkConnections() throws TimeoutException {
jedis.set("foo", "bar"); JedisPool pool = new JedisPool(hnp.host, hnp.port, 2000);
assertEquals("bar", jedis.get("foo")); pool.setResourcesNumber(10);
pool.returnResource(jedis); pool.init();
pool.destroy();
}
@Test Jedis jedis = pool.getResource(200);
public void checkConnectionWithDefaultPort() throws TimeoutException { jedis.auth("foobared");
JedisPool pool = new JedisPool(hnp.host, hnp.port); jedis.set("foo", "bar");
pool.setResourcesNumber(10); assertEquals("bar", jedis.get("foo"));
pool.init(); pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200); @Test
jedis.auth("foobared"); public void checkConnectionWithDefaultPort() throws TimeoutException {
jedis.set("foo", "bar"); JedisPool pool = new JedisPool(hnp.host, hnp.port);
assertEquals("bar", jedis.get("foo")); pool.setResourcesNumber(10);
pool.returnResource(jedis); pool.init();
pool.destroy();
}
@Test Jedis jedis = pool.getResource(200);
public void checkJedisIsReusedWhenReturned() throws TimeoutException { jedis.auth("foobared");
JedisPool pool = new JedisPool(hnp.host, hnp.port); jedis.set("foo", "bar");
pool.setResourcesNumber(1); assertEquals("bar", jedis.get("foo"));
pool.init(); pool.returnResource(jedis);
pool.destroy();
}
Jedis jedis = pool.getResource(200); @Test
jedis.auth("foobared"); public void checkJedisIsReusedWhenReturned() throws TimeoutException {
jedis.set("foo", "0"); JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.returnResource(jedis); pool.setResourcesNumber(1);
pool.init();
jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.auth("foobared"); jedis.auth("foobared");
jedis.incr("foo"); jedis.set("foo", "0");
pool.returnResource(jedis); pool.returnResource(jedis);
pool.destroy();
}
@Test jedis = pool.getResource(200);
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException, jedis.auth("foobared");
IOException { jedis.incr("foo");
JedisPool pool = new JedisPool(hnp.host, hnp.port); pool.returnResource(jedis);
pool.setResourcesNumber(1); pool.destroy();
pool.init(); }
Jedis jedis = pool.getResource(200); @Test
jedis.auth("foobared"); public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
jedis.quit(); IOException {
pool.returnBrokenResource(jedis); JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.auth("foobared"); jedis.auth("foobared");
jedis.incr("foo"); jedis.quit();
pool.returnResource(jedis); pool.returnBrokenResource(jedis);
pool.destroy();
}
@Test(expected = TimeoutException.class) jedis = pool.getResource(200);
public void checkPoolOverflow() throws TimeoutException { jedis.auth("foobared");
JedisPool pool = new JedisPool(hnp.host, hnp.port); jedis.incr("foo");
pool.setResourcesNumber(1); pool.returnResource(jedis);
pool.init(); pool.destroy();
}
Jedis jedis = pool.getResource(200); @Test(expected = TimeoutException.class)
jedis.auth("foobared"); public void checkPoolOverflow() throws TimeoutException {
jedis.set("foo", "0"); JedisPool pool = new JedisPool(hnp.host, hnp.port);
pool.setResourcesNumber(1);
pool.init();
Jedis newJedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
newJedis.auth("foobared"); jedis.auth("foobared");
newJedis.incr("foo"); jedis.set("foo", "0");
}
Jedis newJedis = pool.getResource(200);
newJedis.auth("foobared");
newJedis.incr("foo");
}
} }

View File

@@ -0,0 +1,115 @@
package redis.clients.jedis.tests;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.tests.HostAndPortUtil.HostAndPort;
public class ShardedJedisPoolTest extends Assert {
private static HostAndPort redis1 = HostAndPortUtil.getRedisServers()
.get(0);
private static HostAndPort redis2 = HostAndPortUtil.getRedisServers()
.get(1);
private List<JedisShardInfo> shards;
@Before
public void startUp() throws UnknownHostException, IOException {
shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(redis1.host, redis1.port));
shards.add(new JedisShardInfo(redis2.host, redis2.port));
shards.get(0).setPassword("foobared");
shards.get(1).setPassword("foobared");
Jedis j = new Jedis(shards.get(0));
j.connect();
j.flushAll();
j.disconnect();
j = new Jedis(shards.get(1));
j.connect();
j.flushAll();
j.disconnect();
}
@Test
public void checkConnections() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(10);
pool.init();
ShardedJedis jedis = pool.getResource(200);
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.destroy();
}
@Test
public void checkConnectionWithDefaultPort() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(10);
pool.init();
ShardedJedis jedis = pool.getResource(200);
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.destroy();
}
@Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(1);
pool.init();
ShardedJedis jedis = pool.getResource(200);
jedis.set("foo", "0");
pool.returnResource(jedis);
jedis = pool.getResource(200);
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
@Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(1);
pool.init();
ShardedJedis jedis = pool.getResource(200);
jedis.disconnect();
pool.returnBrokenResource(jedis);
jedis = pool.getResource(200);
jedis.incr("foo");
pool.returnResource(jedis);
pool.destroy();
}
@Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException {
ShardedJedisPool pool = new ShardedJedisPool(shards);
pool.setResourcesNumber(1);
pool.init();
ShardedJedis jedis = pool.getResource(200);
jedis.set("foo", "0");
ShardedJedis newJedis = pool.getResource(200);
newJedis.incr("foo");
}
}