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