package com.zm.order.tool.server.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Description redis配置
 * @Author: ph
 * @Date: 2019/10/30 17:00
 */
@Configuration
@EnableConfigurationProperties(RedisProperty.class)
public class RedisConfig {

    private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);

    @Bean
    @Primary
    public JedisPool jedisPool(@Autowired RedisProperty redisProperty) {
        logger.info("redis地址：" + redisProperty.getHost() + ":" + redisProperty.getPort());
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisProperty.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(redisProperty.getMaxWait());
        JedisPool jedisPool;
        if (StringUtils.isEmpty(redisProperty.getPassword())) {
            jedisPool = new JedisPool(jedisPoolConfig, redisProperty.getHost(), redisProperty.getPort(), redisProperty.getTimeout());
        } else {
            jedisPool = new JedisPool(jedisPoolConfig, redisProperty.getHost(), redisProperty.getPort(), redisProperty.getTimeout(), redisProperty.getPassword());
        }
        logger.info("JedisPool注入成功！！");
        return jedisPool;
    }
}
