package com.roncoo.education.user.common;

import com.roncoo.education.util.tools.JSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class CacheRedis {

    private static final int MINUTE = 5;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 默认缓存5分钟
     */
    public <T> T set(Object key, T t) {
        if (t != null) {
            redisTemplate.opsForValue().set(key, JSUtil.toJSONString(t), MINUTE, TimeUnit.MINUTES);
        }
        return t;
    }

    /**
     * 单位分钟
     */
    public <T> T set(Object key, T t, Integer time) {
        if (t != null) {
            redisTemplate.opsForValue().set(key, JSUtil.toJSONString(t), time, TimeUnit.MINUTES);
        }
        return t;
    }

    public <T> T get(Object key, Class<T> clazz) {
        return JSUtil.parseObject(redisTemplate.opsForValue().get(key).toString(), clazz);
    }

    public void delete(Object key) {
        redisTemplate.delete(key);
    }

}
