package org.springblade.common.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    /**
     * redisson协议前缀
     */
    private static final String SCHEMA_PREFIX = "redis://";

    @Autowired
    private RedisProperties redisProperties;

    @Bean
    public RedissonClient redissonClient() {

        // 创建配置 指定redis地址及节点信息
        Config config = new Config();
        config.useSingleServer().setAddress(SCHEMA_PREFIX + redisProperties.getHost() + ":" + redisProperties.getPort()).setPassword(redisProperties.getPassword());

        // 根据config创建出RedissonClient实例
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}
