Added password to Jedis, JedisPool and ShardedJedis constructor for easier and more efficient usage

This commit is contained in:
Jonathan Leibiusky
2010-09-14 16:43:48 -03:00
parent 708ae8a56e
commit 224555afd2
6 changed files with 53 additions and 21 deletions

View File

@@ -10,6 +10,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import redis.clients.util.ShardInfo;
public class Jedis { public class Jedis {
private Client client = null; private Client client = null;
@@ -26,6 +28,14 @@ public class Jedis {
client.setTimeout(timeout); 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() { public String ping() {
checkIsInMulti(); checkIsInMulti();
client.ping(); client.ping();

View File

@@ -1,11 +1,13 @@
package redis.clients.jedis; package redis.clients.jedis;
import redis.clients.util.FixedResourcePool; import redis.clients.util.FixedResourcePool;
import redis.clients.util.ShardInfo;
public class JedisPool extends FixedResourcePool<Jedis> { public class JedisPool extends FixedResourcePool<Jedis> {
private String host; private String host;
private int port; private int port;
private int timeout; private int timeout;
private String password;
public JedisPool(String host) { public JedisPool(String host) {
this.host = host; this.host = host;
@@ -23,6 +25,20 @@ public class JedisPool extends FixedResourcePool<Jedis> {
this.timeout = timeout; 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 @Override
protected Jedis createResource() { protected Jedis createResource() {
Jedis jedis = new Jedis(this.host, this.port, this.timeout); Jedis jedis = new Jedis(this.host, this.port, this.timeout);
@@ -30,6 +46,9 @@ public class JedisPool extends FixedResourcePool<Jedis> {
while (!done) { while (!done) {
try { try {
jedis.connect(); jedis.connect();
if (password != null) {
jedis.auth(password);
}
done = true; done = true;
} catch (Exception e) { } catch (Exception e) {
try { try {

View File

@@ -352,10 +352,6 @@ public class ShardedJedis extends Sharded<Jedis> {
} }
protected Jedis create(ShardInfo shard) { protected Jedis create(ShardInfo shard) {
Jedis c = new Jedis(shard.getHost(), shard.getPort()); return new Jedis(shard);
if (shard.getPassword() != null) {
c.auth(shard.getPassword());
}
return c;
} }
} }

View File

@@ -13,12 +13,12 @@ import redis.clients.jedis.Protocol;
public class JedisPoolTest extends Assert { public class JedisPoolTest extends Assert {
@Test @Test
public void checkConnections() throws TimeoutException { 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.setResourcesNumber(10);
pool.init(); pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "bar"); jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo")); assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis); pool.returnResource(jedis);
@@ -27,12 +27,12 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void checkConnectionWithDefaultPort() throws TimeoutException { public void checkConnectionWithDefaultPort() throws TimeoutException {
JedisPool pool = new JedisPool("localhost"); JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
2000, "foobared");
pool.setResourcesNumber(10); pool.setResourcesNumber(10);
pool.init(); pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "bar"); jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo")); assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis); pool.returnResource(jedis);
@@ -41,17 +41,16 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void checkJedisIsReusedWhenReturned() throws TimeoutException { public void checkJedisIsReusedWhenReturned() throws TimeoutException {
JedisPool pool = new JedisPool("localhost"); JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
2000, "foobared");
pool.setResourcesNumber(1); pool.setResourcesNumber(1);
pool.init(); pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "0"); jedis.set("foo", "0");
pool.returnResource(jedis); pool.returnResource(jedis);
jedis = pool.getResource(200); jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.incr("foo"); jedis.incr("foo");
pool.returnResource(jedis); pool.returnResource(jedis);
pool.destroy(); pool.destroy();
@@ -60,17 +59,16 @@ public class JedisPoolTest extends Assert {
@Test @Test
public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException, public void checkPoolRepairedWhenJedisIsBroken() throws TimeoutException,
IOException { IOException {
JedisPool pool = new JedisPool("localhost"); JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
2000, "foobared");
pool.setResourcesNumber(1); pool.setResourcesNumber(1);
pool.init(); pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.quit(); jedis.quit();
pool.returnBrokenResource(jedis); pool.returnBrokenResource(jedis);
jedis = pool.getResource(200); jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.incr("foo"); jedis.incr("foo");
pool.returnResource(jedis); pool.returnResource(jedis);
pool.destroy(); pool.destroy();
@@ -78,16 +76,15 @@ public class JedisPoolTest extends Assert {
@Test(expected = TimeoutException.class) @Test(expected = TimeoutException.class)
public void checkPoolOverflow() throws TimeoutException { public void checkPoolOverflow() throws TimeoutException {
JedisPool pool = new JedisPool("localhost"); JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT,
2000, "foobared");
pool.setResourcesNumber(1); pool.setResourcesNumber(1);
pool.init(); pool.init();
Jedis jedis = pool.getResource(200); Jedis jedis = pool.getResource(200);
jedis.auth("foobared");
jedis.set("foo", "0"); jedis.set("foo", "0");
Jedis newJedis = pool.getResource(200); Jedis newJedis = pool.getResource(200);
newJedis.auth("foobared");
newJedis.incr("foo"); newJedis.incr("foo");
} }
} }

View File

@@ -6,8 +6,10 @@ import java.util.Map;
import org.junit.Test; import org.junit.Test;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.commands.JedisCommandTestBase; import redis.clients.jedis.tests.commands.JedisCommandTestBase;
import redis.clients.util.RedisOutputStream; import redis.clients.util.RedisOutputStream;
import redis.clients.util.ShardInfo;
public class JedisTest extends JedisCommandTestBase { public class JedisTest extends JedisCommandTestBase {
@Test @Test
@@ -31,4 +33,11 @@ public class JedisTest extends JedisCommandTestBase {
assertEquals(hash, jedis.hgetAll("foo")); 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");
}
} }

View File

@@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Protocol;
public class PoolBenchmark { public class PoolBenchmark {
private static final int TOTAL_OPERATIONS = 100000; private static final int TOTAL_OPERATIONS = 100000;
@@ -25,7 +26,7 @@ public class PoolBenchmark {
// withoutPool(); // withoutPool();
withPool(); withPool();
long elapsed = System.currentTimeMillis() - t; 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 { private static void withoutPool() throws InterruptedException {
@@ -60,7 +61,8 @@ public class PoolBenchmark {
} }
private static void withPool() throws InterruptedException { 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.setResourcesNumber(50);
pool.setDefaultPoolWait(1000000); pool.setDefaultPoolWait(1000000);
pool.init(); pool.init();
@@ -73,7 +75,6 @@ public class PoolBenchmark {
for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) { for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS;) {
try { try {
Jedis j = pool.getResource(); Jedis j = pool.getResource();
j.auth("foobared");
final String key = "foo" + i; final String key = "foo" + i;
j.set(key, key); j.set(key, key);
j.get(key); j.get(key);