package com.platform.utils;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SetArgs;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

/**
 * @author abnerhou
 * @date 2020/10/14 10:08
 * @desciption
 */
@Slf4j
public class LettuceRedisClientUtils {

    public static void setStrKeyValue(String key , String value , long expire){
        // <1> 创建单机连接的连接信息
        RedisURI redisUri = RedisURI.builder()
                .withHost(ResourceUtil.getConfigByName("spring.redis.host"))
                .withPort(new Integer(ResourceUtil.getConfigByName("spring.redis.port")))
                .withPassword(ResourceUtil.getConfigByName("spring.redis.password"))
                .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
                .build();
        // <2> 创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        // <3> 创建线程安全的连接
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        // <4> 创建同步命令
        RedisCommands<String, String> redisCommands = connection.sync();
        SetArgs setArgs = SetArgs.Builder.nx().ex(expire);
        String result = redisCommands.set(key, value, setArgs);
        log.info("设置的redis数据结果为:{}" , result);
        // <5> 关闭连接
        connection.close();
        // <6> 关闭客户端
        redisClient.shutdown();
    }

    public static void setAndExpStrKeyValue(String key , String value , long expire){
        // <1> 创建单机连接的连接信息
        RedisURI redisUri = RedisURI.builder()
                .withHost(ResourceUtil.getConfigByName("spring.redis.host"))
                .withPort(new Integer(ResourceUtil.getConfigByName("spring.redis.port")))
                .withPassword(ResourceUtil.getConfigByName("spring.redis.password"))
                .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
                .build();
        // <2> 创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        // <3> 创建线程安全的连接
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        // <4> 创建同步命令
        RedisCommands<String, String> redisCommands = connection.sync();
        SetArgs setArgs = SetArgs.Builder.ex(expire);
        String result = redisCommands.set(key, value, setArgs);
        log.info("设置的redis数据结果为:{}" , result);
        // <5> 关闭连接
        connection.close();
        // <6> 关闭客户端
        redisClient.shutdown();
    }


    public static Object getValueByKey(String key){
        RedisURI redisUri = RedisURI.builder()                    // <1> 创建单机连接的连接信息
                .withHost(ResourceUtil.getConfigByName("spring.redis.host"))
                .withPort(new Integer(ResourceUtil.getConfigByName("spring.redis.port")))
                .withPassword(ResourceUtil.getConfigByName("spring.redis.password"))
                .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
                .build();
        // <2> 创建客户端
        RedisClient redisClient = RedisClient.create(redisUri);
        // <3> 创建线程安全的连接
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        // <4> 创建同步命令
        RedisCommands<String, String> redisCommands = connection.sync();

        return redisCommands.get(key);
    }
}
