Java Redis Connection Pooling Jedis tool class

Source: Internet
Author: User
Tags delete key

Import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Redis.clients.jedis.jedis;import Redis.clients.jedis.jedispool;import Redis.clients.jedis.jedispoolconfig;import Java.io.IOException;import Java.io.inputstream;import Java.util.properties;public class Myjedispool {private final static Logger Logger = Loggerf    Actory.getlogger (Myjedispool.class);    private static Jedispool Readpool = null;    private static Jedispool Writepool = null;            Static code initialization pool configuration static {try{Properties props = new properties ();            InputStream in = MyJedisPool.class.getResourceAsStream ("/redis.properties");            Props.load (in);            Create Jedis pool configuration instance jedispoolconfig config = new Jedispoolconfig ();            Set the pool configuration item Value Config.setmaxtotal (Integer.valueof (Props.getproperty ("jedis.pool.maxActive"));            Config.setmaxidle (Integer.valueof (Props.getproperty ("Jedis.pool.maxIdle")); Config.setmaxwaitmillis (Long.valueof (Props.getproPerty ("jedis.pool.maxWait"));            Config.settestonborrow (Boolean.valueof (Props.getproperty ("Jedis.pool.testOnBorrow"));            Config.settestonreturn (Boolean.valueof (Props.getproperty ("Jedis.pool.testOnReturn")); Instantiate the Jedis pool based on configuration Readpool = new Jedispool (config, props.getproperty ("Redisreadurl"), Integer.valueof (PROPS.GETPR            Operty ("Redisreadport")); Writepool = new Jedispool (config, props.getproperty ("Rediswriteurl"), Integer.valueof (Props.getproperty ("        Rediswriteport ")));        }catch (IOException e) {logger.info ("Redis Connection Pool exception", e);    }}/** get Jedis object */public static Jedis Getreadjedisobject () {return readpool.getresource ();    }/** Get Jedis object */public static Jedis Getwritejedisobject () {return writepool.getresource (); /** return Jedis Object */public static void Returnjedisojbect (Jedis Jedis) {if (Jedis! = null) {Jedis.clo        SE (); }    }}

Import Redis.clients.jedis.jedis;import Java.util.set;public class Redisutils {/** * get all keys in the hash table * @param na        Me * @return */public static set<string> Gethashallkey (String name) {Jedis Jedis = null;            try {Jedis = Myjedispool.getreadjedisobject ();        return Jedis.hkeys (name);        }catch (Exception e) {e.printstacktrace ();        }finally {myjedispool.returnjedisojbect (Jedis);    } return null; */** * from the redis hash table * @param hashname * @param key * @return */public static String Gethash        KV (String hashname,string key) {Jedis Jedis = null;            try {Jedis = Myjedispool.getreadjedisobject ();        Return Jedis.hget (Hashname, key);        }catch (Exception e) {e.printstacktrace ();        }finally {myjedispool.returnjedisojbect (Jedis);    } return null; }/** * Delete hash table key value pair * @param hashname    * @param key */public static Long delhashkv (String hashname,string key) {Jedis Jedis = null;            try {Jedis = Myjedispool.getwritejedisobject ();        Return Jedis.hdel (Hashname,key);        }catch (Exception e) {e.printstacktrace ();        }finally {myjedispool.returnjedisojbect (Jedis);    } return null; }/** * Store hash Table key value pair * @param hashname * @param key * @param value * * public static Long SETHASHKV (String hashname,string key,string value)        {Jedis Jedis = null;            try {Jedis = Myjedispool.getwritejedisobject ();        Return Jedis.hset (Hashname,key,value);        }catch (Exception e) {e.printstacktrace ();        }finally {myjedispool.returnjedisojbect (Jedis);    } return null;         }/** * Delete key value pairs * @param k * @return */public static Long delkv (String k) {Jedis Jedis = null; try {JEdis = Myjedispool.getwritejedisobject ();        Return Jedis.del (k);        }catch (Exception e) {e.printstacktrace ();        }finally {myjedispool.returnjedisojbect (Jedis);    } return null;        }/** * Set key value pair * Permanent * @param k * @param v */public static string Setkv (string k, string v) {        Jedis Jedis = null;            try {Jedis = Myjedispool.getwritejedisobject ();        Return Jedis.set (k, v);        }catch (Exception e) {e.printstacktrace ();        }finally {myjedispool.returnjedisojbect (Jedis);    } return null; }/** * Set key value pair * * @param k * @param v */public static string Setkv (String k,int Second, string V        ) {Jedis Jedis = null;            try {Jedis = Myjedispool.getwritejedisobject ();        Return Jedis.setex (K,second, v);        }catch (Exception e) {e.printstacktrace ();           }finally { Myjedispool.returnjedisojbect (Jedis);    } return null; }/** * takes value by key * * @param k * @return */public static string Getkv (String k) {J        Edis Jedis = null;            try {Jedis = Myjedispool.getreadjedisobject ();        Return Jedis.get (k);        }catch (Exception e) {e.printstacktrace ();        }finally {myjedispool.returnjedisojbect (Jedis);    } return null; }}

  

Java Redis Connection Pooling Jedis tool class

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.