You need to use the following jar package
<!--https://Mvnrepository.com/artifact/redis.clients/jedis- <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> < version>2.8. 1</version> </dependency> <!--https://mvnrepository.com/ Artifact/commons-pool/commons-pool- <dependency> <groupid>commons-pool</ groupid> <artifactId>commons-pool</artifactId> <version>1.6</ Version> </dependency>
1. Establishing a connection pool
Package Com.spring.wzy;import Redis.clients.jedis.jedis;import redis.clients.jedis.jedispool;import Redis.clients.jedis.JedisPoolConfig; Public classJedispoolutil {/** * single case with double lock form **/ Private Static volatileJedispool Jedispool =NULL; PrivateJedispoolutil () {} Public StaticJedispool getjedispoolinstance () {if(NULL==Jedispool) {synchronized (jedispoolutil).class) { if(NULL==Jedispool) { //genericobjectpoolconfig poolconfig = new Jedispoolconfig ();Jedispoolconfig Poolconfig =NewJedispoolconfig (); Poolconfig.setmaxactive ( +);//Maximum number of instancesPoolconfig.setmaxwaitmillis ( -* +);//Maximum wait timePoolconfig.setmaxidle ( -);//Most idle instancesPoolconfig.settestonborrow (true);//Check connectivity availability When you get an instanceJedispool =NewJedispool (Poolconfig,"192.168.23.128",6379); } } } returnJedispool; } Public Static voidrelease (Jedispool jedispool, Jedis Jedis) {if(NULL!=Jedis) { //jedis.close ();Jedispool.returnresourceobject (Jedis); } }}
2. Usage examples
Public Static voidMain (string[] args) throws interruptedexception {Jedispool Jedispool=jedispoolutil.getjedispoolinstance (); Jedis J=NULL; Try{J=Jedispool.getresource (); //j.select (0);//Select the database, the default is library # No. 0//J.flushall ();//emptying the database//J.del ("key");//Delete key//j.set ("K1", "v1");//J.set ("K2", "V2");//J.set ("K2", "V3");//the new value overrides the old value//System.out.println (j.exists ("K2"));//back to Ture/false//System.out.println (j.exists ("K1", "K3"));//returns the number of existing//System.out.println (New String (J.get ("K1". GetBytes () )));//System.out.println (J.get ("K2")); //set<string> keys = J.keys ("*");//For (String k:keys) {//System.out.println (k);// } //j.move ("K1", 2);//cut the K1 into Library No. 2nd .//System.out.println (J.expire ("K2", "ten)");//set the survival time of key (s), perform a successful return of 1, otherwise return 0//Thread.Sleep (+);//System.out.println (J.ttl ("K2"));//to view the expiration time of a key }Catch(Exception e) {e.printstacktrace (); }finally{jedispoolutil.release (Jedispool, J); }
Java Operations Redis