Use of Jedis (Java+redis) pool

Source: Internet
Author: User
Tags deprecated

Try it today. The use of connection pool jedispool in Jedis. The code is as follows:

 PackageCom.myapp.jedis.pooldemo;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;/*** Created by Baidu on 16/10/18.*/ Public classTestpool {Private StaticJedispool pool =NULL;  Public StaticJedispool Getpool () {if(Pool = =NULL) {jedispoolconfig config=NewJedispoolconfig (); Config.setmaxtotal (500); Config.setmaxidle (5); Config.setmaxwaitmillis (1000*10); //whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available;Config.settestonborrow (true); //new Jedispool (config, ADDR, PORT, TIMEOUT, AUTH);Pool =NewJedispool (config, "[IP]", 8379, 10000, "[Auth]"); }        returnPool; }     Public synchronized StaticJedis getresource () {if(Pool = =NULL) {Pool=Getpool (); }        returnPool.getresource (); }    //return to Connection pool//Deprecated//when you're done, redis.close ()    /*Public static void Returnresource (Jedis redis) {if (Redis! = null) {Pool.returnresource (Redis)        ; }    }    */     Public Static voidMain (string[] args) {Jedis Redis=NULL; intloop = 1;  while(Loop < 20) {            Try {                LongStart =System.currenttimemillis (); Redis=getresource (); Redis.set ("K1", "v1"); String ret= Redis.get ("K1"); LongEnd =System.currenttimemillis (); System.out.printf ("Get ret from Redis:%s with%d millis\n", ret, end-start); } finally {                if(Redis! =NULL) {redis.close (); }} Loop++; }    }}

Among them, there is a function returnresource has been deprecated, now Jedis the Close method rewrite, with Jedis.close to release resources.

Ran 20 times, the result of the operation is as follows:

Get ret from REDIS:V1 with564 MillisGet ret from Redis:v1 with235 MillisGet ret from Redis:v1 with225millisget ret from Redis:v1 with214millisget ret from Redis:v1 with210millisget ret from Redis:v1 with232millisget ret from Redis:v1 with209millisget ret from Redis:v1 with211millisget ret from Redis:v1 with239millisget ret from Redis:v1 with207millisget ret from Redis:v1 with215millisget ret from Redis:v1 with223millisget ret from Redis:v1 with291millisget ret from Redis:v1 with220millisget ret from Redis:v1 with214millisget ret from Redis:v1 with219millisget ret from Redis:v1 with257millisget ret from Redis:v1 with214millisget ret from Redis:v1 with211Millisprocess finished with exit code0

As you can see, the first time is more than 500 milliseconds, followed by more than 200 milliseconds, the speed has improved.

Use of Jedis (Java+redis) pool

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.