Add ability to specify database number in pool config
This commit is contained in:
@@ -8,30 +8,30 @@ import redis.clients.util.Pool;
|
||||
|
||||
public class JedisPool extends Pool<Jedis> {
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,
|
||||
final String host) {
|
||||
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT,
|
||||
null);
|
||||
public JedisPool(final Config poolConfig, final String host) {
|
||||
this(poolConfig, host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
|
||||
}
|
||||
|
||||
public JedisPool(String host, int port) {
|
||||
super(new Config(), new JedisFactory(host, port,
|
||||
Protocol.DEFAULT_TIMEOUT, null));
|
||||
this(new Config(), host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
|
||||
}
|
||||
|
||||
public JedisPool(final Config poolConfig, final String host, int port,
|
||||
int timeout, final String password) {
|
||||
super(poolConfig, new JedisFactory(host, port, timeout, password));
|
||||
this(poolConfig, host, port, timeout, password, Protocol.DEFAULT_DATABASE);
|
||||
}
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,
|
||||
final String host, final int port) {
|
||||
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null);
|
||||
public JedisPool(final Config poolConfig, final String host, final int port) {
|
||||
this(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE);
|
||||
}
|
||||
|
||||
public JedisPool(final GenericObjectPool.Config poolConfig,
|
||||
final String host, final int port, final int timeout) {
|
||||
this(poolConfig, host, port, timeout, null);
|
||||
public JedisPool(final Config poolConfig, final String host, final int port, final int timeout) {
|
||||
this(poolConfig, host, port, timeout, null, Protocol.DEFAULT_DATABASE);
|
||||
}
|
||||
|
||||
public JedisPool(final Config poolConfig, final String host, int port, int timeout, final String password,
|
||||
final int database) {
|
||||
super(poolConfig, new JedisFactory(host, port, timeout, password, database));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,14 +42,16 @@ public class JedisPool extends Pool<Jedis> {
|
||||
private final int port;
|
||||
private final int timeout;
|
||||
private final String password;
|
||||
private final int database;
|
||||
|
||||
public JedisFactory(final String host, final int port,
|
||||
final int timeout, final String password) {
|
||||
final int timeout, final String password, final int database) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
this.password = password;
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public Object makeObject() throws Exception {
|
||||
@@ -59,6 +61,10 @@ public class JedisPool extends Pool<Jedis> {
|
||||
if (null != this.password) {
|
||||
jedis.auth(this.password);
|
||||
}
|
||||
if( database != 0 ) {
|
||||
jedis.select(database);
|
||||
}
|
||||
|
||||
return jedis;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ public final class Protocol {
|
||||
|
||||
public static final int DEFAULT_PORT = 6379;
|
||||
public static final int DEFAULT_TIMEOUT = 2000;
|
||||
public static final int DEFAULT_DATABASE = 0;
|
||||
|
||||
public static final String CHARSET = "UTF-8";
|
||||
|
||||
|
||||
@@ -95,4 +95,22 @@ public class JedisPoolTest extends Assert {
|
||||
pool.returnResource(jedis);
|
||||
pool.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonDefaultDatabase() {
|
||||
JedisPool pool0 = new JedisPool(new JedisPoolConfig(), hnp.host,
|
||||
hnp.port, 2000, "foobared");
|
||||
Jedis jedis0 = pool0.getResource();
|
||||
jedis0.set("foo", "bar");
|
||||
assertEquals( "bar", jedis0.get("foo") );
|
||||
pool0.returnResource(jedis0);
|
||||
pool0.destroy();
|
||||
|
||||
JedisPool pool1 = new JedisPool(new JedisPoolConfig(), hnp.host,
|
||||
hnp.port, 2000, "foobared", 1);
|
||||
Jedis jedis1 = pool1.getResource();
|
||||
assertNull( jedis1.get("foo") );
|
||||
pool1.returnResource(jedis0);
|
||||
pool1.destroy();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user