Java Operations Redis

Source: Internet
Author: User
Tags delete key


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

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.