Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to do connection pooling with redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report