package com.brframework.commoncache.config;

import com.brframework.commoncache.core.FastJsonRedisCodec;
import com.google.common.base.Strings;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redisson 配置
 * @author xu
 * @date 2019年9月4日22:56:12
 */
@Configuration
@ConfigurationProperties(value = "spring.redis")
@Data
public class RedissonConfig {

    @Autowired
    RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient (){
        String address = "redis://" + redisProperties.getHost() + ":" + redisProperties.getPort();
        Config config = new Config();
        config.setTransportMode(TransportMode.NIO);
        config.useSingleServer()
                .setDatabase(redisProperties.getDatabase())
                .setAddress(address)
                .setPassword(redisProperties.getPassword());
        config.setCodec(new FastJsonRedisCodec());
        return Redisson.create(config);
    }


}
