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