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 ();
}
}
Util class for Redis, for guidance