Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to do connection pooling with redis

Shulou Source: shulou.com Published: 2022-06-01 04:59:30 09月22日 Update

This article will explain in detail how to do connection pooling in redis. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Redis can also be pooled like a database for each link request. The specific applications are as follows:

Package redisOne;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;public class RedisPoolUtil {private static JedisPool jedisPool = null; static {JedisPoolConfig config = new JedisPoolConfig (); / / whether to block when the connection is exhausted. False reports an exception and ture blocks until it times out. Default is true config.setBlockWhenExhausted (true). / / whether LIFO is enabled, default true config.setLifo (true); / / maximum number of idle connections, default 8 config.setMaxIdle (8); / / maximum number of connections, default 8 config.setMaxTotal (8) / / get the maximum number of milliseconds to wait for a connection (if set to BlockWhenExhausted when blocking), throw an exception if the timeout is less than zero: block an uncertain time, default-1 config.setMaxWaitMillis (- 1); / / default the minimum idle time for evicted connections is 1800000 milliseconds (30 minutes) config.setMinEvictableIdleTimeMillis (1800000) / / minimum number of idle connections. Default is 0 config.setMinIdle (0); / / the maximum number of evictions for each eviction check is negative: 1/abs (n), default is 3 config.setNumTestsPerEvictionRun (3) / / after how long the object is idle, when the idle time > this value and the idle connection > the maximum number of idle, it is no longer based on MinEvictableIdleTimeMillis judgment (default eviction policy) config.setSoftMinEvictableIdleTimeMillis (1800000); / / check the validity when getting the connection. Default is false config.setTestOnBorrow (false). / / check validity when idle. Default is false config.setTestWhileIdle (false); / / if the time interval for ejection scan is negative, the eviction thread is not run, default is-1 config.setTimeBetweenEvictionRunsMillis (- 1); jedisPool = new JedisPool (config, "localhost", 6379) } public static String getOneKey (String key) {String value = jedisPool.getResource () .get (key); return value;} public static void setOneKey (String key, String value) {jedisPool.getResource () .set (key, value) } public static void main (String [] args) {RedisPoolUtil.setOneKey ("liuc", "liuc"); System.out.println (RedisPoolUtil.getOneKey ("liuc"));}}

For those who use spring for bean management, you can use the following configuration

This is the end of the article on "how to do connection pooling in redis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Idle maximum time blocking article check minimum valid more valid negative good practical content backward object that is data database number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB OPPO Reno Xiaomi MySQL