add url support
This commit is contained in:
@@ -2,6 +2,7 @@ package redis.clients.jedis;
|
||||
|
||||
import static redis.clients.jedis.Protocol.toByteArray;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@@ -20,7 +21,16 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
protected Client client = null;
|
||||
|
||||
public BinaryJedis(final String host) {
|
||||
client = new Client(host);
|
||||
URI uri = URI.create(host);
|
||||
if (uri.getScheme() != null && uri.getScheme().equals("redis")) {
|
||||
client = new Client(uri.getHost(), uri.getPort());
|
||||
client.auth(uri.getUserInfo().split(":", 2)[1]);
|
||||
client.getStatusCodeReply();
|
||||
client.select(Integer.parseInt(uri.getPath().split("/", 2)[1]));
|
||||
client.getStatusCodeReply();
|
||||
} else {
|
||||
client = new Client(host);
|
||||
}
|
||||
}
|
||||
|
||||
public BinaryJedis(final String host, final int port) {
|
||||
@@ -38,6 +48,14 @@ public class BinaryJedis implements BinaryJedisCommands {
|
||||
client.setPassword(shardInfo.getPassword());
|
||||
}
|
||||
|
||||
public BinaryJedis(URI uri) {
|
||||
client = new Client(uri.getHost(), uri.getPort());
|
||||
client.auth(uri.getUserInfo().split(":", 2)[1]);
|
||||
client.getStatusCodeReply();
|
||||
client.select(Integer.parseInt(uri.getPath().split("/", 2)[1]));
|
||||
client.getStatusCodeReply();
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
checkIsInMulti();
|
||||
client.ping();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@@ -29,6 +30,10 @@ public class Jedis extends BinaryJedis implements JedisCommands {
|
||||
super(shardInfo);
|
||||
}
|
||||
|
||||
public Jedis(URI uri) {
|
||||
super(uri);
|
||||
}
|
||||
|
||||
public String ping() {
|
||||
checkIsInMulti();
|
||||
client.ping();
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.commons.pool.BasePoolableObjectFactory;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
||||
|
||||
import redis.clients.util.Pool;
|
||||
@@ -16,9 +19,30 @@ public class JedisPool extends Pool<Jedis> {
|
||||
}
|
||||
|
||||
public JedisPool(final String host) {
|
||||
this(host, Protocol.DEFAULT_PORT);
|
||||
URI uri = URI.create(host);
|
||||
if (uri.getScheme() != null && uri.getScheme().equals("redis")) {
|
||||
String h = uri.getHost();
|
||||
int port = uri.getPort();
|
||||
String password = uri.getUserInfo().split(":", 2)[1];
|
||||
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
||||
this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
|
||||
Protocol.DEFAULT_TIMEOUT, password, database), new Config());
|
||||
} else {
|
||||
this.internalPool = new GenericObjectPool(new JedisFactory(host,
|
||||
Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null,
|
||||
Protocol.DEFAULT_DATABASE), new Config());
|
||||
}
|
||||
}
|
||||
|
||||
public JedisPool(final URI uri) {
|
||||
String h = uri.getHost();
|
||||
int port = uri.getPort();
|
||||
String password = uri.getUserInfo().split(":", 2)[1];
|
||||
int database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
|
||||
this.internalPool = new GenericObjectPool(new JedisFactory(h, port,
|
||||
Protocol.DEFAULT_TIMEOUT, password, database), new Config());
|
||||
}
|
||||
|
||||
public JedisPool(final Config poolConfig, final String host, int port,
|
||||
int timeout, final String password) {
|
||||
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import redis.clients.util.ShardInfo;
|
||||
import redis.clients.util.Sharded;
|
||||
|
||||
public class JedisShardInfo extends ShardInfo<Jedis> {
|
||||
public String toString() {
|
||||
return host + ":" + port + "*" + getWeight();
|
||||
return host + ":" + port + "*" + getWeight();
|
||||
}
|
||||
|
||||
private int timeout;
|
||||
@@ -15,67 +17,83 @@ public class JedisShardInfo extends ShardInfo<Jedis> {
|
||||
private String name = null;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
return port;
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host) {
|
||||
this(host, Protocol.DEFAULT_PORT);
|
||||
super(Sharded.DEFAULT_WEIGHT);
|
||||
URI uri = URI.create(host);
|
||||
if (uri.getScheme() != null && uri.getScheme().equals("redis")) {
|
||||
this.host = uri.getHost();
|
||||
this.port = uri.getPort();
|
||||
this.password = uri.getUserInfo().split(":", 2)[1];
|
||||
} else {
|
||||
this.host = host;
|
||||
this.port = Protocol.DEFAULT_PORT;
|
||||
}
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, String name) {
|
||||
this(host, Protocol.DEFAULT_PORT, name);
|
||||
this(host, Protocol.DEFAULT_PORT, name);
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port) {
|
||||
this(host, port, 2000);
|
||||
this(host, port, 2000);
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port, String name) {
|
||||
this(host, port, 2000, name);
|
||||
this(host, port, 2000, name);
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port, int timeout) {
|
||||
this(host, port, timeout, Sharded.DEFAULT_WEIGHT);
|
||||
this(host, port, timeout, Sharded.DEFAULT_WEIGHT);
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port, int timeout, String name) {
|
||||
this(host, port, timeout, Sharded.DEFAULT_WEIGHT);
|
||||
this.name = name;
|
||||
this(host, port, timeout, Sharded.DEFAULT_WEIGHT);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port, int timeout, int weight) {
|
||||
super(weight);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
super(weight);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public JedisShardInfo(URI uri) {
|
||||
super(Sharded.DEFAULT_WEIGHT);
|
||||
this.host = uri.getHost();
|
||||
this.port = uri.getPort();
|
||||
this.password = uri.getUserInfo().split(":", 2)[1];
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String auth) {
|
||||
this.password = auth;
|
||||
this.password = auth;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Jedis createResource() {
|
||||
return new Jedis(this);
|
||||
return new Jedis(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,12 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
import redis.clients.jedis.exceptions.JedisException;
|
||||
|
||||
public abstract class Pool<T> {
|
||||
private final GenericObjectPool internalPool;
|
||||
protected GenericObjectPool internalPool;
|
||||
|
||||
protected Pool() {
|
||||
this.internalPool = null;
|
||||
}
|
||||
|
||||
public Pool(final GenericObjectPool.Config poolConfig,
|
||||
PoolableObjectFactory factory) {
|
||||
this.internalPool = new GenericObjectPool(factory, poolConfig);
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
||||
import org.junit.Assert;
|
||||
@@ -123,4 +126,28 @@ public class JedisPoolTest extends Assert {
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startWithUrlString() {
|
||||
Jedis j = new Jedis("localhost", 6380);
|
||||
j.auth("foobared");
|
||||
j.select(2);
|
||||
j.set("foo", "bar");
|
||||
JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2");
|
||||
Jedis jedis = pool.getResource();
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startWithUrl() throws URISyntaxException {
|
||||
Jedis j = new Jedis("localhost", 6380);
|
||||
j.auth("foobared");
|
||||
j.select(2);
|
||||
j.set("foo", "bar");
|
||||
JedisPool pool = new JedisPool(new URI("redis://:foobared@localhost:6380/2"));
|
||||
Jedis jedis = pool.getResource();
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -17,56 +19,78 @@ import redis.clients.util.SafeEncoder;
|
||||
public class JedisTest extends JedisCommandTestBase {
|
||||
@Test
|
||||
public void useWithoutConnecting() {
|
||||
Jedis jedis = new Jedis("localhost");
|
||||
jedis.auth("foobared");
|
||||
jedis.dbSize();
|
||||
Jedis jedis = new Jedis("localhost");
|
||||
jedis.auth("foobared");
|
||||
jedis.dbSize();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkBinaryData() {
|
||||
byte[] bigdata = new byte[1777];
|
||||
for (int b = 0; b < bigdata.length; b++) {
|
||||
bigdata[b] = (byte) ((byte) b % 255);
|
||||
}
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("data", SafeEncoder.encode(bigdata));
|
||||
byte[] bigdata = new byte[1777];
|
||||
for (int b = 0; b < bigdata.length; b++) {
|
||||
bigdata[b] = (byte) ((byte) b % 255);
|
||||
}
|
||||
Map<String, String> hash = new HashMap<String, String>();
|
||||
hash.put("data", SafeEncoder.encode(bigdata));
|
||||
|
||||
String status = jedis.hmset("foo", hash);
|
||||
assertEquals("OK", status);
|
||||
assertEquals(hash, jedis.hgetAll("foo"));
|
||||
String status = jedis.hmset("foo", hash);
|
||||
assertEquals("OK", status);
|
||||
assertEquals(hash, jedis.hgetAll("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectWithShardInfo() {
|
||||
JedisShardInfo shardInfo = new JedisShardInfo("localhost",
|
||||
Protocol.DEFAULT_PORT);
|
||||
shardInfo.setPassword("foobared");
|
||||
Jedis jedis = new Jedis(shardInfo);
|
||||
jedis.get("foo");
|
||||
JedisShardInfo shardInfo = new JedisShardInfo("localhost",
|
||||
Protocol.DEFAULT_PORT);
|
||||
shardInfo.setPassword("foobared");
|
||||
Jedis jedis = new Jedis(shardInfo);
|
||||
jedis.get("foo");
|
||||
}
|
||||
|
||||
@Test(expected = JedisConnectionException.class)
|
||||
public void timeoutConnection() throws Exception {
|
||||
jedis = new Jedis("localhost", 6379, 15000);
|
||||
jedis.auth("foobared");
|
||||
jedis.configSet("timeout", "1");
|
||||
// we need to sleep a long time since redis check for idle connections
|
||||
// every 10 seconds or so
|
||||
Thread.sleep(20000);
|
||||
jedis.hmget("foobar", "foo");
|
||||
jedis = new Jedis("localhost", 6379, 15000);
|
||||
jedis.auth("foobared");
|
||||
jedis.configSet("timeout", "1");
|
||||
// we need to sleep a long time since redis check for idle connections
|
||||
// every 10 seconds or so
|
||||
Thread.sleep(20000);
|
||||
jedis.hmget("foobar", "foo");
|
||||
}
|
||||
|
||||
@Test(expected = JedisDataException.class)
|
||||
public void failWhenSendingNullValues() {
|
||||
jedis.set("foo", null);
|
||||
jedis.set("foo", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReconnectToSameDB() throws IOException {
|
||||
jedis.select(1);
|
||||
jedis.set("foo", "bar");
|
||||
jedis.getClient().getSocket().shutdownInput();
|
||||
jedis.getClient().getSocket().shutdownOutput();
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
jedis.select(1);
|
||||
jedis.set("foo", "bar");
|
||||
jedis.getClient().getSocket().shutdownInput();
|
||||
jedis.getClient().getSocket().shutdownOutput();
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startWithUrlString() {
|
||||
Jedis j = new Jedis("localhost", 6380);
|
||||
j.auth("foobared");
|
||||
j.select(2);
|
||||
j.set("foo", "bar");
|
||||
Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2");
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startWithUrl() throws URISyntaxException {
|
||||
Jedis j = new Jedis("localhost", 6380);
|
||||
j.auth("foobared");
|
||||
j.select(2);
|
||||
j.set("foo", "bar");
|
||||
Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"));
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -167,4 +169,60 @@ public class ShardedJedisPoolTest extends Assert {
|
||||
assertEquals(actual, c1);
|
||||
assertEquals(fails, c2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startWithUrlString() {
|
||||
Jedis j = new Jedis("localhost", 6380);
|
||||
j.auth("foobared");
|
||||
j.set("foo", "bar");
|
||||
|
||||
j = new Jedis("localhost", 6379);
|
||||
j.auth("foobared");
|
||||
j.set("foo", "bar");
|
||||
|
||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||
shards.add(new JedisShardInfo("redis://:foobared@localhost:6380"));
|
||||
shards.add(new JedisShardInfo("redis://:foobared@localhost:6379"));
|
||||
|
||||
Config redisConfig = new Config();
|
||||
ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
|
||||
|
||||
Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]);
|
||||
|
||||
Jedis jedis = jedises[0];
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
|
||||
jedis = jedises[1];
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startWithUrl() throws URISyntaxException {
|
||||
Jedis j = new Jedis("localhost", 6380);
|
||||
j.auth("foobared");
|
||||
j.set("foo", "bar");
|
||||
|
||||
j = new Jedis("localhost", 6379);
|
||||
j.auth("foobared");
|
||||
j.set("foo", "bar");
|
||||
|
||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||
shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));
|
||||
shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379")));
|
||||
|
||||
Config redisConfig = new Config();
|
||||
ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
|
||||
|
||||
Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]);
|
||||
|
||||
Jedis jedis = jedises[0];
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
|
||||
jedis = jedises[1];
|
||||
assertEquals("PONG", jedis.ping());
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user