package com.slodon.b2b2c.starter.redisson;

import com.slodon.b2b2c.core.util.StringUtil;
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.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redis相关配置
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SlodonRedissonProperties.class)
public class RedissonLockAutoConfig {


    @Bean
    public SlodonLock slodonLock(SlodonRedissonProperties properties) {
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer()
                .setAddress(properties.getHost() + ":" + properties.getPort())
                .setDatabase(properties.getDatabase());
        if(!StringUtil.isEmpty(properties.getPassword())){
            singleServerConfig.setPassword(properties.getPassword());
        }
        RedissonClient redissonClient = Redisson.create(config);
        return new SlodonLock(redissonClient);
    }

}
