Redis generic connection class (simple and detailed)

 Generic connection class Redis

Connected environment based on the following class to write

  • JDK 1.8
  • Jedis 3.2.0

The following is a specific connection type

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Mokerson_For_TGZ
 */
public class JedisPoolUtil {
    /**
     * Jedis对象只能出现一个
     */
    private static volatile JedisPool jedisPool = null;
    /**
     * 构造方法私有化
     */
    private JedisPoolUtil() {
    }

    public static Jedis getJedisPoolInstance() {
        // 如果JedisPool的实例没有初始化,那么就加载
        if (null == jedisPool) {
            if (null == jedisPool) {
                JedisPoolConfig poolConfig = new JedisPoolConfig();
                // 最大的空闲数
                poolConfig.setMaxIdle(32);
                // 最大的连接数
                poolConfig.setMaxTotal(1000);
                // 连接超时时间
                poolConfig.setMaxWaitMillis(15000);
                poolConfig.setTestOnBorrow(true);
                jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379);
            }
        }
        // 返回Jedis连接
        return jedisPool.getResource();
    }

    /**
     * 关闭Jedis连接,释放资源
     */
    public static void close(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }
}

Tips: If the address is connected to a non-native, note the connection address, port, password and firewall.

Published 36 original articles · won praise 3 · Views 5655

Guess you like

Origin blog.csdn.net/TanGuozheng_Java/article/details/104085577