package com.bizzan.bitrade.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

@Configuration
public class RedisionConfig {

    @Value("${spring.redis.host}")
    private String redisHost;


    @Value("${spring.redis.port}")
    private String redisPort;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.setLockWatchdogTimeout(30000L);
        config.setTransportMode(TransportMode.NIO);
        SingleServerConfig singleServerConfig = config.useSingleServer();
        //可以用"rediss://"来启用SSL连接
        singleServerConfig
                .setAddress("redis://" + redisHost + ":" + redisPort)
                .setSubscriptionConnectionMinimumIdleSize(8)
                .setSubscriptionConnectionPoolSize(8)
                        .setConnectionMinimumIdleSize(8)
                        .setConnectionPoolSize(8);
//                .setPingConnectionInterval(1000);
        if(password.trim().length() > 0)
        	singleServerConfig.setPassword(password);
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}
