package com.zksy.data.config;

import cn.hutool.core.util.StrUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {
    @Value("${redisson.address}")
    private String address;
    @Value("${redisson.pw}")
    private String pw;

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();
        if (StrUtil.isNotBlank(pw)) {
            config.useSingleServer()
                    .setAddress(address)
                    .setPassword(pw)
                    .setDatabase(0);
        } else {
            config.useSingleServer()
                    .setAddress(address)
                    .setDatabase(0);
        }
        return Redisson.create(config);
    }
}