Redis's Util class, asking for guidance

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.