public class RedisPoolFactory {
private static HashMap<String, JedisPool> poolFactory = new HashMap<String, JedisPool>();
public static JedisPool getPool(String hostname, int port) {
String key = hostname + port;
if (!poolFactory.containsKey(key)) {
synchronized (RedisPoolFactory.class) {
if (!poolFactory.containsKey(key)) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//资源池中最大连接数
jedisPoolConfig.setMaxTotal(200);
//资源池允许最大空闲的连接数
jedisPoolConfig.setMaxIdle(30);
//默认值是-1 表示永不超时 不建议使用
jedisPoolConfig.setMaxWaitMillis(10000);
//返回连接时,是否提前进行 validate 操作
jedisPoolConfig.setTestOnReturn( true );
jedisPoolConfig.setTestWhileIdle( true );
JedisPool jedisPool = new JedisPool(jedisPoolConfig, hostname, port, 3000);
poolFactory.put(key, jedisPool);
}
}
}
return poolFactory.get(key);
}
}