Redis client - Jedis connection pool

The address of the original text is updated, and the reading effect is better!

Redis Client - Jedis Connection Pool | CoderMast Programming Mast icon-default.png?t=N5F7https://www.codermast.com/database/redis/jedis-connection-pool.html

Jedis itself is not thread-safe, and frequently creating and destroying connections will cause performance loss, so we use the Jedis connection pool instead of the direct connection method of Jedis.

  • Configure Jedis connection pool

public class JedisConnectionFactory{
    private static final JedisPool jedisPool;

    static {
        JedisPoolConfig jedisPollConfig = new JedisPoolConfig();

        // 最大连接,这里设置为 8
        jedisPollConfig.setMaxTotal(8);
        // 最大空闲连接,这里设置为 8
        jedisPollConfig.setMaxIdle(8);
        // 最小空闲连接,这里设置为 0 
        jedisPollConfig.setMaxIdle(0);
        // 设置最长等待时间,单位 ms
        jedisPollConfig.setMaxWaitMillis(200);

        jedisPool = new JedisPool(jedisPoolConfig,"192.168.100.100",6379,1000,"codermast");
    }

    // 获取Jedis对象
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

Notice

The Jedis connection pool may not be used in a single-threaded environment, but the Jedis connection pool must be used in a multi-threaded environment, and it is more reliable to use the Jedis connection pool. We do not need to pay attention to issues such as resource acquisition and release, and can focus on business logic.

Guess you like

Origin blog.csdn.net/qq_33685334/article/details/131252617