package com.dgtly.wxportal.utils;

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



public class JedisPoolUtil {
    private static volatile JedisPool jedisPool = null;
    private static final String HOST="127.0.0.1" ;
    private static final int PORT = 6379;
    private static final String PASSWORD = "Ssbprod#1207";
    private static final int TIMEOUT = 2000;


    private JedisPoolUtil() {}
    public static JedisPool getPool() {
        if (jedisPool == null) {
            synchronized (JedisPoolUtil.class) {
                if (jedisPool == null) {
                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxTotal(200);       // 最大连接数
                    config.setMaxIdle(50);         // 最大空闲连接
                    config.setMinIdle(10);         // 最小空闲连接
                    config.setTestOnBorrow(true);  // 借出连接时校验
                    config.setTestOnReturn(true);  // 归还连接时校验
                    config.setMaxWaitMillis(1000); // 等待连接超时时间(ms)

                    jedisPool = new JedisPool(
                            config, HOST, PORT, TIMEOUT, PASSWORD
                    );
                }
            }
        }
        return jedisPool;
    }

    public static Jedis getResource() {
        return getPool().getResource();
    }

    public static void close(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}


