maven需要的依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
工具类
public class JedisPoolUtil {
private static volatile JedisPool jedisPool=null;
private JedisPoolUtil(){};
public static JedisPool getJedisPoolInstance(){
if(jedisPool==null){
synchronized (JedisPoolUtil.class) {
if(jedisPool==null){
JedisPoolConfig poolConfig=new JedisPoolConfig();
poolConfig.setMaxTotal(1000);
poolConfig.setMaxWaitMillis(1000*100);
poolConfig.setMaxIdle(32);
poolConfig.setTestOnBorrow(true);
jedisPool=new JedisPool(poolConfig, "127.0.0.1",6379);
}
}
}
return jedisPool;
}
@SuppressWarnings("deprecation")
//用完后还给连接池
public static void release(JedisPool jedisPool,Jedis jedis){
if(null != jedis){
jedisPool.returnResourceObject(jedis);
}
}
}
测试类
public class Test {
public static void main(String[] args) {
JedisPool jedisPool=JedisPoolUtil.getJedisPoolInstance();
Jedis jedis=jedisPool.getResource();
jedis.set("keychm", "cyz");
JedisPoolUtil.release(jedisPool, jedis);
}
}