package com.mmall.common;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Created by zhang
*/
public class RedisPool {
private static JedisPool pool;//jedis连接池
private static Integer maxTotal = 20; //最大连接数
private static Integer maxIdle = 10;//在jedispool中最大的idle状态(空闲的)的jedis实例的个数
private static Integer minIdle = 2;//在jedispool中最小的idle状态(空闲的)的jedis实例的个数
private static Boolean testOnBorrow =true;//在borrow一个jedis实例的时候,是否要进行验证操作,如果赋值true。则得到的jedis实例肯定是可以用的。
private static Boolean testOnReturn =false;//在return一个jedis实例的时候,是否要进行验证操作,如果赋值true。则放回jedispool的jedis实例肯定是可以用的。
private static String redisIp = 127.0.0.1;//redis的IP地址
private static Integer redisPort = 6379;//redis的端口
private static void initPool(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
config.setBlockWhenExhausted(true);//连接耗尽的时候,是否阻塞,false会抛出异常,true阻塞直到超时。默认为true。
pool = new JedisPool(config,redisIp,redisPort,1000*2);
}
static{
initPool();
}
public static Jedis getJedis(){
return pool.getResource();
}
public static void returnBrokenResource(Jedis jedis){
pool.returnBrokenResource(jedis);
}
public static void returnResource(Jedis jedis){
pool.returnResource(jedis);
}
public static void main(String[] args) {
Jedis jedis = pool.getResource();
jedis.set("key","value");
returnResource(jedis);
pool.destroy();//临时调用,销毁连接池中的所有连接
System.out.println("program is end");
}
}
Redis链接池
最新推荐文章于 2023-05-08 16:11:32 发布