package com.yuzhouwan.bigdata.redis;

import com.yuzhouwan.common.util.DynamicPropUtils;
import com.yuzhouwan.common.util.StrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Copyright @ 2017 yuzhouwan.com
 * All right reserved.
 * Function：Redis Cluster Conn Pool
 *
 * @author Benedict Jin
 * @since 2017/7/18
 */
public class RedisClusterConnPool {

    private static final Logger _log = LoggerFactory.getLogger(RedisClusterConnPool.class);
    public static final String PROJECT_NAME = "REDIS_STORE";

    private static JedisCluster pool;

    public RedisClusterConnPool() {
        init(DynamicPropUtils.getInstance());
    }

    public RedisClusterConnPool(DynamicPropUtils DP) {
        init(DP);
    }

    private void init(DynamicPropUtils DP) {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(1000);
        config.setMinIdle(50);
        config.setMaxIdle(100);
        config.setMaxWaitMillis(6 * 1000);
        config.setTestOnBorrow(true);
        Set<HostAndPort> jedisClusterNodes = new HashSet<>();

        Object clusterListObj = DP.get(PROJECT_NAME, "redis.cluster.list");
        String clusterList;
        if (clusterListObj == null || StrUtils.isEmpty(clusterList = clusterListObj.toString())) {
            String error = String.format("Cannot get [%s-redis.cluster.list] from Dynamic PropUtils!", PROJECT_NAME);
            _log.error(error);
            throw new RuntimeException(error);
        }
        String[] hostAndPort;
        for (String clusters : clusterList.split(",")) {
            hostAndPort = clusters.split(":");
            jedisClusterNodes.add(new HostAndPort(hostAndPort[0], Integer.valueOf(hostAndPort[1])));
        }
        pool = new JedisCluster(jedisClusterNodes, 2000, 100, config);
    }

    public String put(String key, String value) {
        try {
            return pool.set(key, value);
        } catch (Exception e) {
            _log.error("", e);
            return null;
        }
    }

    public String get(String key) {
        try {
            return pool.get(key);
        } catch (Exception e) {
            _log.error("", e);
            return null;
        }
    }

    public Long putSet(String key, String... values) {
        try {
            return pool.sadd(key, values);
        } catch (Exception e) {
            _log.error("", e);
            return null;
        }
    }

    public Set<String> getSet(String key) {
        try {
            return pool.smembers(key);
        } catch (Exception e) {
            _log.error("", e);
            return null;
        }
    }

    public void close() {
        try {
            if (pool != null) pool.close();
        } catch (IOException e) {
            _log.error("", e);
        }
    }
}

@@ -661,16 +661,55 @@
 .class);
+%0A%0A    private static JedisCluster pool;
 %0A    pub
@@ -758,53 +758,16 @@
 DIS_
-STORE%22;%0A%0A    private static JedisCluster pool
+CLUSTER%22
 ;%0A%0A 
@@ -983,305 +983,8 @@
 ) %7B%0A
-        JedisPoolConfig config = new JedisPoolConfig();%0A        config.setMaxTotal(1000);%0A        config.setMinIdle(50);%0A        config.setMaxIdle(100);%0A        config.setMaxWaitMillis(6 * 1000);%0A        config.setTestOnBorrow(true);%0A        Set%3CHostAndPort%3E jedisClusterNodes = new HashSet%3C%3E();%0A%0A
     
@@ -1418,16 +1418,78 @@
 ndPort;%0A
+        Set%3CHostAndPort%3E jedisClusterNodes = new HashSet%3C%3E();%0A
         
@@ -1687,32 +1687,254 @@
 %5D)));%0A        %7D%0A
+        JedisPoolConfig conf = new JedisPoolConfig();%0A        conf.setMaxTotal(1000);%0A        conf.setMinIdle(50);%0A        conf.setMaxIdle(100);%0A        conf.setMaxWaitMillis(6 * 1000);%0A        conf.setTestOnBorrow(true);%0A
         pool = n
@@ -1971,26 +1971,13 @@
 des,
- 2000, 100,
  conf
-ig
 );%0A 

Improve redisClusterConnPool