import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 外部反序列化
*
*/
@Component
public class RedisTools<T>
{
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 存储
*
* @param key
* @param instance 类对象
* @param timeOut 超时时间 单位/小时
*/
public void set(String key, T instance,
int timeOut){
String value =
JSON.toJSONString(instance);
redisTemplate.opsForValue().set(key, value, timeOut, TimeUnit.HOURS);
}
/**
* 获取
*
* @param key
* @param clazz
*/
public T get(String key, Class<T>
clazz){
String value =
redisTemplate.boundValueOps(key).get();
if(value ==
null)
return null;
return JSON.parseObject(value, clazz);
}
}
转载于:https://www.cnblogs.com/Uzai/p/10985567.html