package org.jeecg.modules.adweb.common.util;

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

public class JedisUtil {
    private static JedisPool jedisPool;

    public static void initJedisPool(String jedisIp, int jedisPort, String jedisPassword) {

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(256);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        config.setTestWhileIdle(true);
        config.setNumTestsPerEvictionRun(-1);
        if (jedisPassword == null || jedisPassword.isEmpty()) {
            jedisPool = new JedisPool(config, jedisIp, jedisPort, 60000);
        } else {
            jedisPool = new JedisPool(config, jedisIp, jedisPort, 60000, jedisPassword);
        }
    }

    /**
     * 存储REDIS队列 顺序存储
     *
     * @param key reids键名
     * @param value 键值
     */
    public static void lpush(byte[] key, byte[] value) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.lpush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 返还到连接池
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 存储REDIS队列 反向存储
     *
     * @param key reids键名
     * @param value 键值
     */
    public static void rpush(byte[] key, byte[] value) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.rpush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 返还到连接池
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 获取队列数据
     *
     * @param key 键名
     * @return
     */
    public static byte[] rpop(byte[] key) {

        byte[] bytes = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            bytes = jedis.rpop(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 返还到连接池
            if (jedis != null) {
                jedis.close();
            }
        }
        return bytes;
    }

    /**
     * 发送消息
     *
     * @param channel
     * @param message
     */
    public static void publishMsg(String channel, String message) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.publish(channel, message);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 返还到连接池
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 接收消息
     *
     * @param jedisPubSub
     * @param channels
     */
    public static void subscribeMsg(JedisPubSub jedisPubSub, String channels) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.subscribe(jedisPubSub, channels);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 返还到连接池
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
