package org.jeecg.modules.redission;

import cn.hutool.core.util.StrUtil;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class RedissonConfig {
 
    private String host;
    private String port;
    private String password;
 
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://"+host+":"+port).setPassword(password).setDatabase(8);
        SingleServerConfig serverConfig = config.useSingleServer();
        serverConfig.setAddress("redis://" + host +":6379");
        if (StrUtil.isNotEmpty(password)){
            serverConfig.setPassword(password);
        }
        //设置连接redis的心跳间隔（30s/次）
        serverConfig.setPingConnectionInterval(30000);
        config.useSingleServer();
        config.setEventLoopGroup(new NioEventLoopGroup(16));
        return Redisson.create(config);
    }
}