Parameter | Description | Suggestion |
setMaxTotal | Maximum connections in the connection pool | It is subjected to the factors like the volume of concurrent business, access delay, and maximum connections. |
setMaxIdle | Maximum idle connections in the connection pool | Set it to a value same as setMaxTotal |
setMinIdle | Minimum idle connections in the connection pool | Set it the same as setMaxTotal |
timeout | Timeout period | It is set based on your business model and the network linkage performance. When network latency is low and your businesses are very sensitive to service latency, set the value from 50 to 100 ms. If your business has high latency tolerance or large volume of key-value data, set it to 500 ms or 1,000 ms. |
setTestOnBorrow |
Whether to test the connection when obtaining it in a connection pool | When the value is true , connection.isValid() will be called for the connection test. This ensures the availability of the obtained connection while consuming QPS performance.When the value is false , connection test will not be performed. The speed of obtaining a connection can be improved, but available connections may not be obtained. |
setTestOnReturn | Whether to perform verification when the connection is returned to the connection pool. | When the value is true , connection.isValid() will be called when returning the connection,
ensuring the connections are valid.When the value is false , connection test will not be performed. The speed of returning a connection can be improved, but the returned connections may be unavailable. |
JedisPoolConfig config = new JedisPoolConfig();// Maximum idle connections, which can't exceed the maximum connections of Redis instancesconfig.setMaxIdle(200);// Maximum connections, which can't exceed the maximum connections of Redis instancesconfig.setMaxTotal(200);// Minimum idle connections in the connection poolconfig.setMinIdle(20);// Maximum wait time when the connections are used upconfig.setMaxWaitMillis(3000);// When an object is obtained from the connection pool, a ping check will be performed first. If the check fails, the object will be removed and destroyed.config.setTestOnBorrow(false);// When a connection is returned, a check will be performed first. Once the check fails, the connection will be terminated.config.setTestOnReturn(false);// Set the connection pool mode to “queue”config.setLifo(false);// Set the minimum connectionsconfig.setTimeBetweenEvictionRunsMillis(3000);// Replace the values of "host" and "password" with the connection address and password of the instance respectivelyString host = "192.xx.xx.195";String password = "123ad6aq";// Read/write timeout in msint timeout = 2000;int port = 6379;JedisPool pool = new JedisPool(config,host,port,timeout,password);Jedis jedis = null;boolean broken = false;try{jedis = pool.getResource();/// ... do stuff here ... for examplejedis.set("redis", "tencent");String foobar = jedis.get("redis");jedis.zadd("tec", 0, "a");jedis.zadd("tec", 0, "b");Set < String > sose = jedis.zrange("tec", 0, -1);}catch(Exception e){broken = true;}finally{if(broken){pool.returnBrokenResource(jedis);}else if(jedis != null){pool.returnResource(jedis);}}
この記事はお役に立ちましたか?