首页 > 代码库 > Redis的util类,求指导
Redis的util类,求指导
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtils {
private static final Log log = LogFactory.getLog(RedisUtils.class);
private static JedisPool rPool = null;
private static JedisPool wPool = null;
static{
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(BaseConstants.REDIS_MAX_TOTAL);
config.setMaxIdle(BaseConstants.REDIS_MAX_IDLE);
config.setMaxWaitMillis(BaseConstants.REDIS_TIME_OUT);
config.setTestOnBorrow(BaseConstants.REDIS_TESTONBORROW);
rPool = new JedisPool(config, Config.INSTANCE.getConfigValue("redis.r.host"),
Integer.parseInt(Config.INSTANCE.getConfigValue("redis.r.port")));
wPool = new JedisPool(config, Config.INSTANCE.getConfigValue("redis.w.host"),
Integer.parseInt(Config.INSTANCE.getConfigValue("redis.w.port")));
}
/**
* Get A New Redis Connection From Read Redis Pool To Read Data!
* @return
*/
public static Jedis getReadJedis(){
Jedis jedis = rPool.getResource();
if(StringUtils.notEmpty(Config.INSTANCE.getConfigValue("redis.r.pwd"))){
jedis.auth(Config.INSTANCE.getConfigValue("redis.r.pwd"));
}
log.info("Get A New Redis Connection From Read Redis Pool To Read Data!");
return jedis;
}
/**
* Get A New Redis Connection From Write Redis Pool To Output Data!
* @return
*/
public static Jedis getWriteJedis(){
Jedis jedis = wPool.getResource();
if(StringUtils.notEmpty(Config.INSTANCE.getConfigValue("redis.w.pwd"))){
jedis.auth(Config.INSTANCE.getConfigValue("redis.w.pwd"));
}
log.info("Get A New Redis Connection From Write Redis Pool To Output Data!");
return jedis;
}
/**
* Get A New Redis Connection appointing the db index From Write Redis Pool To Output Data!
* @param Index
* @return
*/
public static Jedis getWriteJedis(int Index){
Jedis jedis = getWriteJedis();
jedis.select(Index);
return jedis;
}
/**
* Realeas Read Redis Connection!
* @param jedis the redis to be realeas
*/
public static void releasReadJedis(Jedis rJedis){
try {
if(rJedis!=null){
rJedis.disconnect();
rPool.returnResource(rJedis);
log.info("Realeas Read Redis Connection!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Realeas Write Redis Connection!
* @param jedis the redis to be realeas
*/
public static void releasWriteJedis(Jedis wJedis){
try {
if(wJedis!=null){
wJedis.disconnect();
wPool.returnResource(wJedis);
log.info("Realeas Write Redis Connection!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* destory read redis pool
*/
public static void destoryReadPool(){
if(rPool != null){
rPool.destroy();
log.info("Destory Read Redis Pool!");
}
}
/**
* destory write redis pool
*/
public static void destoryWritePool(){
if(wPool != null){
wPool.destroy();
log.info("Destory Write Redis Pool!");
}
}
/**
* destory all redis pool
*/
public static void destoryAllPool(){
destoryReadPool();
destoryWritePool();
}
}
Redis的util类,求指导