package xiaoqu.home.open.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


/**
 * Redis配置
 * @author smile
 */
@Configuration
@Order(value=0)
public class RedisConfig {

    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.password}")
    private String password;
    @Value("${redis.pool.max-total}")
    private int maxTotal;
    @Value("${redis.pool.max-wait}")
    private int maxWait;
    @Value("${redis.pool.max-idle}")
    private int maxIdle;
    @Value("${redis.pool.min-idle}")
    private int minIdle;

    @Bean(name = "redisTemplate4")
    @Order(value=0)
    public RedisTemplate<String, String> redisTemplate4() {
    	LettuceConnectionFactory lettuceConnectionFactory = db4Factory();
        return getRedisTemplate(lettuceConnectionFactory);
    }
    @Bean(name = "redisTemplate5")
    @Order(value=0)
    public RedisTemplate<String, String> redisTemplate5() {
    	LettuceConnectionFactory lettuceConnectionFactory = db5Factory();
        return getRedisTemplate(lettuceConnectionFactory);
    }
    @Bean(name = "db4Factory")
    @Primary
    public LettuceConnectionFactory db4Factory(){
    	LettuceClientConfiguration lettuceClientConfiguration = getClientConfig();
    	RedisStandaloneConfiguration redisStandaloneConfiguration = getRedisConfig(4);
        return new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);
    }
    @Bean(name = "db5Factory")
    public LettuceConnectionFactory db5Factory(){
    	LettuceClientConfiguration lettuceClientConfiguration = getClientConfig();
    	RedisStandaloneConfiguration redisStandaloneConfiguration = getRedisConfig(5);
        return new LettuceConnectionFactory(redisStandaloneConfiguration, lettuceClientConfiguration);
    }
    private RedisStandaloneConfiguration getRedisConfig(int dbNo) {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        config.setPassword(password);
        config.setDatabase(dbNo);
        return config;
    }

    private LettuceClientConfiguration getClientConfig() {
        GenericObjectPoolConfig<?> poolConfig = new GenericObjectPoolConfig<Object>();
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxWaitMillis(maxWait);
        return LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build();
    }

    public RedisTemplate<String, String> getRedisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, String> template = new RedisTemplate<String, String>();
        template.setValueSerializer((RedisSerializer<String>) new StringRedisSerializer());
		template.setKeySerializer((RedisSerializer<String>) new StringRedisSerializer());
		template.setHashKeySerializer((RedisSerializer<String>) new StringRedisSerializer());
		template.setHashValueSerializer((RedisSerializer<String>) new StringRedisSerializer());
		template.setConnectionFactory(factory);
		template.afterPropertiesSet();
        return template;
    }

}