package com.ai.da.common.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: dangweijian
 * @description: redis缓存工具
 * @create: 2021-01-08 9:23
 **/
public final class RedisCacheUtils {

    private static RedisTemplate<String, Object> getRedisTemplate() {
        return SpringUtils.getBean("redisTemplate");
    }

    private static <T> RedisTemplate<String, T> getRedisTemplate(Class<T> clazz) {
        return SpringUtils.getBean("redisTemplate");
    }

    private static <T> RedisTemplate<String, List<T>> getListRedisTemplate(Class<T> clazz) {
        return SpringUtils.getBean("redisTemplate");
    }

    public static <T> T get(String key, Class<T> clazz) {
        return getRedisTemplate(clazz).opsForValue().get(key);
    }

    public static <T> List<T> getList(String key, Class<T> clazz) {
        return getListRedisTemplate(clazz).opsForValue().get(key);
    }

    public static void set(String key, Object value) {
        getRedisTemplate().opsForValue().set(key, value);
    }

    public static void set(String key, Object value, long time, TimeUnit unit) {
        getRedisTemplate().opsForValue().set(key, value, time, unit);
    }

    public static boolean delete(String key) {
        if (StrUtil.isNotEmpty(key)) {
            return Boolean.TRUE.equals(getRedisTemplate().delete(key));
        }
        return false;
    }
}
