redis学习之JedisPool+JedisCluster
原创
©著作权归作者所有:来自51CTO博客作者wx58fde585c569f的原创作品,请联系作者获取转载授权,否则将追究法律责任
redis学习之JedisPool+JedisCluster
JedisPool
public class RedisPool {
private static JedisPool pool = null;
static{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(5);
config.setMaxWaitMillis(100*1000);
config.setTestOnBorrow(true);
pool = new JedisPool(config,"192.168.235.100",6379,10000);
}
public static Jedis getJedis(){
return pool.getResource();
}
public static void release(Jedis jedis){
if(jedis!=null)
jedis.close();
}
}
@Test
public void testStock() throws InterruptedException {
Jedis jedis = RedisPool.getJedis();
try {
int stock = Integer.parseInt(jedis.get("stock"));
System.out.println(stock);
} finally {
RedisPool.release(jedis);
}
}
JedisCluster
public class JedisClusterPool {
private static JedisCluster jedisCluster;
public static JedisCluster getJedisCluster(){
if(jedisCluster==null){
synchronized (JedisClusterPool.class){
if(jedisCluster==null){
Set<HostAndPort> hostAndPortsSet = new HashSet<HostAndPort>();
hostAndPortsSet.add(new HostAndPort("192.168.235.100", 6380));
hostAndPortsSet.add(new HostAndPort("192.168.235.100", 6381));
hostAndPortsSet.add(new HostAndPort("192.168.235.100", 6382));
hostAndPortsSet.add(new HostAndPort("192.168.235.100", 6390));
hostAndPortsSet.add(new HostAndPort("192.168.235.100", 6391));
hostAndPortsSet.add(new HostAndPort("192.168.235.100", 6392));
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(5);
config.setMaxWaitMillis(100*1000);
config.setTestOnBorrow(true);
jedisCluster = new JedisCluster(hostAndPortsSet, config);
}
}
}
return jedisCluster;
}
}
@Test
public void testCluster() throws InterruptedException {
System.out.println(JedisClusterPool.getJedisCluster().get("stock"));
}