package com.docus.server.common.auth;

import com.docus.infrastructure.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

//redis string类型操作类
public class RedisStringService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    //文设置值，保存为String类型
    public void setValue(String key, Object value) {
        stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(value));
    }

    //设置值，保存为String类型

    public void setValue(String key, Object value, long timeout, TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key, JsonUtils.toJson(value), timeout, unit);
    }
//获取String类型值，并自动反序列化为对象

    public <T> T getValue(String key, Class<T> type) {
        String value = stringRedisTemplate.opsForValue().get(key);
        return JsonUtils.fromJson(value, type);
    }
//重新设置过期时间

    public void setExpire(String key, long timeout, TimeUnit unit) {
        stringRedisTemplate.expire(key, timeout, unit);
    }

    //删除
    public void delete(String key) {
        stringRedisTemplate.delete(key);
    }
//key是否存在

    public Boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }
}
