add url support
This commit is contained in:
@@ -2,6 +2,7 @@ package redis.clients.jedis;
|
|||||||
|
|
||||||
import static redis.clients.jedis.Protocol.toByteArray;
|
import static redis.clients.jedis.Protocol.toByteArray;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
@@ -20,8 +21,17 @@ public class BinaryJedis implements BinaryJedisCommands {
|
|||||||
protected Client client = null;
|
protected Client client = null;
|
||||||
|
|
||||||
public BinaryJedis(final String host) {
|
public BinaryJedis(final String 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);
|
client = new Client(host);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public BinaryJedis(final String host, final int port) {
|
public BinaryJedis(final String host, final int port) {
|
||||||
client = new Client(host, port);
|
client = new Client(host, port);
|
||||||
@@ -38,6 +48,14 @@ public class BinaryJedis implements BinaryJedisCommands {
|
|||||||
client.setPassword(shardInfo.getPassword());
|
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() {
|
public String ping() {
|
||||||
checkIsInMulti();
|
checkIsInMulti();
|
||||||
client.ping();
|
client.ping();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package redis.clients.jedis;
|
package redis.clients.jedis;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
@@ -29,6 +30,10 @@ public class Jedis extends BinaryJedis implements JedisCommands {
|
|||||||
super(shardInfo);
|
super(shardInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Jedis(URI uri) {
|
||||||
|
super(uri);
|
||||||
|
}
|
||||||
|
|
||||||
public String ping() {
|
public String ping() {
|
||||||
checkIsInMulti();
|
checkIsInMulti();
|
||||||
client.ping();
|
client.ping();
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package redis.clients.jedis;
|
package redis.clients.jedis;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import org.apache.commons.pool.BasePoolableObjectFactory;
|
import org.apache.commons.pool.BasePoolableObjectFactory;
|
||||||
|
import org.apache.commons.pool.impl.GenericObjectPool;
|
||||||
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
||||||
|
|
||||||
import redis.clients.util.Pool;
|
import redis.clients.util.Pool;
|
||||||
@@ -16,7 +19,28 @@ public class JedisPool extends Pool<Jedis> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public JedisPool(final String host) {
|
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,
|
public JedisPool(final Config poolConfig, final String host, int port,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package redis.clients.jedis;
|
package redis.clients.jedis;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import redis.clients.util.ShardInfo;
|
import redis.clients.util.ShardInfo;
|
||||||
import redis.clients.util.Sharded;
|
import redis.clients.util.Sharded;
|
||||||
|
|
||||||
@@ -23,7 +25,16 @@ public class JedisShardInfo extends ShardInfo<Jedis> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public JedisShardInfo(String host) {
|
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) {
|
public JedisShardInfo(String host, String name) {
|
||||||
@@ -54,6 +65,13 @@ public class JedisShardInfo extends ShardInfo<Jedis> {
|
|||||||
this.timeout = timeout;
|
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() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
|
|||||||
import redis.clients.jedis.exceptions.JedisException;
|
import redis.clients.jedis.exceptions.JedisException;
|
||||||
|
|
||||||
public abstract class Pool<T> {
|
public abstract class Pool<T> {
|
||||||
private final GenericObjectPool internalPool;
|
protected GenericObjectPool internalPool;
|
||||||
|
|
||||||
|
protected Pool() {
|
||||||
|
this.internalPool = null;
|
||||||
|
}
|
||||||
|
|
||||||
public Pool(final GenericObjectPool.Config poolConfig,
|
public Pool(final GenericObjectPool.Config poolConfig,
|
||||||
PoolableObjectFactory factory) {
|
PoolableObjectFactory factory) {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package redis.clients.jedis.tests;
|
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;
|
||||||
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
import org.apache.commons.pool.impl.GenericObjectPool.Config;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@@ -123,4 +126,28 @@ public class JedisPoolTest extends Assert {
|
|||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
pool.destroy();
|
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;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -69,4 +71,26 @@ public class JedisTest extends JedisCommandTestBase {
|
|||||||
jedis.getClient().getSocket().shutdownOutput();
|
jedis.getClient().getSocket().shutdownOutput();
|
||||||
assertEquals("bar", jedis.get("foo"));
|
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;
|
package redis.clients.jedis.tests;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -167,4 +169,60 @@ public class ShardedJedisPoolTest extends Assert {
|
|||||||
assertEquals(actual, c1);
|
assertEquals(actual, c1);
|
||||||
assertEquals(fails, c2);
|
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