Added password to Jedis, JedisPool and ShardedJedis constructor for easier and more efficient usage
This commit is contained in:
@@ -10,6 +10,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import redis.clients.util.ShardInfo;
|
||||
|
||||
public class Jedis {
|
||||
private Client client = null;
|
||||
|
||||
@@ -26,6 +28,14 @@ public class Jedis {
|
||||
client.setTimeout(timeout);
|
||||
}
|
||||
|
||||
public Jedis(ShardInfo shardInfo) {
|
||||
client = new Client(shardInfo.getHost(), shardInfo.getPort());
|
||||
client.setTimeout(shardInfo.getTimeout());
|
||||
if (shardInfo.getPassword() != null) {
|
||||
this.auth(shardInfo.getPassword());
|
||||
}
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
checkIsInMulti();
|
||||
client.ping();
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import redis.clients.util.FixedResourcePool;
|
||||
import redis.clients.util.ShardInfo;
|
||||
|
||||
public class JedisPool extends FixedResourcePool<Jedis> {
|
||||
private String host;
|
||||
private int port;
|
||||
private int timeout;
|
||||
private String password;
|
||||
|
||||
public JedisPool(String host) {
|
||||
this.host = host;
|
||||
@@ -23,6 +25,20 @@ public class JedisPool extends FixedResourcePool<Jedis> {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public JedisPool(String host, int port, int timeout, String password) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public JedisPool(ShardInfo shardInfo) {
|
||||
this.host = shardInfo.getHost();
|
||||
this.port = shardInfo.getPort();
|
||||
this.timeout = shardInfo.getTimeout();
|
||||
this.password = shardInfo.getPassword();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Jedis createResource() {
|
||||
Jedis jedis = new Jedis(this.host, this.port, this.timeout);
|
||||
@@ -30,6 +46,9 @@ public class JedisPool extends FixedResourcePool<Jedis> {
|
||||
while (!done) {
|
||||
try {
|
||||
jedis.connect();
|
||||
if (password != null) {
|
||||
jedis.auth(password);
|
||||
}
|
||||
done = true;
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
|
||||
@@ -352,10 +352,6 @@ public class ShardedJedis extends Sharded<Jedis> {
|
||||
}
|
||||
|
||||
protected Jedis create(ShardInfo shard) {
|
||||
Jedis c = new Jedis(shard.getHost(), shard.getPort());
|
||||
if (shard.getPassword() != null) {
|
||||
c.auth(shard.getPassword());
|
||||
}
|
||||
return c;
|
||||
return new Jedis(shard);
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,12 @@ import redis.clients.jedis.Protocol;
|
||||
public class JedisPoolTest extends Assert {
|
||||
@Test
|
||||
public void checkConnections() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT, 2000);
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(10);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
@@ -27,12 +27,12 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void checkConnectionWithDefaultPort() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(10);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
@@ -41,17 +41,16 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void checkJedisIsReusedWhenReturned() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(1);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "0");
|
||||
pool.returnResource(jedis);
|
||||
|
||||
jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.incr("foo");
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
@@ -60,17 +59,16 @@ public class JedisPoolTest extends Assert {
|
||||
@Test
|
||||
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
|
||||
IOException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(1);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.quit();
|
||||
pool.returnBrokenResource(jedis);
|
||||
|
||||
jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.incr("foo");
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
@@ -78,16 +76,15 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
@Test(expected = TimeoutException.class)
|
||||
public void checkPoolOverflow() throws TimeoutException {
|
||||
JedisPool pool = new JedisPool("localhost");
|
||||
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
|
||||
2000, "foobared");
|
||||
pool.setResourcesNumber(1);
|
||||
pool.init();
|
||||
|
||||
Jedis jedis = pool.getResource(200);
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "0");
|
||||
|
||||
Jedis newJedis = pool.getResource(200);
|
||||
newJedis.auth("foobared");
|
||||
newJedis.incr("foo");
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,10 @@ import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.Protocol;
|
||||
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
import redis.clients.util.ShardInfo;
|
||||
|
||||
public class JedisTest extends JedisCommandTestBase {
|
||||
@Test
|
||||
@@ -31,4 +33,11 @@ public class JedisTest extends JedisCommandTestBase {
|
||||
assertEquals(hash, jedis.hgetAll("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectWithShardInfo() {
|
||||
ShardInfo shardInfo = new ShardInfo("localhost", Protocol.DEFAULT_PORT);
|
||||
shardInfo.setPassword("foobared");
|
||||
Jedis jedis = new Jedis(shardInfo);
|
||||
jedis.get("foo");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.Protocol;
|
||||
|
||||
public class PoolBenchmark {
|
||||
private static final int TOTAL_OPERATIONS = 100000;
|
||||
@@ -25,7 +26,7 @@ public class PoolBenchmark {
|
||||
// withoutPool();
|
||||
withPool();
|
||||
long elapsed = System.currentTimeMillis() - t;
|
||||
System.out.println(((1000 * 3 * TOTAL_OPERATIONS) / elapsed) + " ops");
|
||||
System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");
|
||||
}
|
||||
|
||||
private static void withoutPool() throws InterruptedException {
|
||||
@@ -60,7 +61,8 @@ public class PoolBenchmark {
|
||||
}
|
||||
|
||||
private static void withPool() throws InterruptedException {
|
||||
final JedisPool pool = new JedisPool("localhost");
|
||||
final JedisPool pool = new JedisPool("localhost",
|
||||
Protocol.DEFAULT_PORT, 2000, "foobared");
|
||||
pool.setResourcesNumber(50);
|
||||
pool.setDefaultPoolWait(1000000);
|
||||
pool.init();
|
||||
@@ -73,7 +75,6 @@ public class PoolBenchmark {
|
||||
for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) {
|
||||
try {
|
||||
Jedis j = pool.getResource();
|
||||
j.auth("foobared");
|
||||
final String key = "foo" + i;
|
||||
j.set(key, key);
|
||||
j.get(key);
|
||||
|
||||
Reference in New Issue
Block a user