package com.syjh.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.syjh.common.exception.BusinessException;
import com.syjh.consts.Const;
import com.syjh.entity.User;
import com.syjh.utils.ajax.JsonUtil;
import com.syjh.utils.ajax.Result;
public class RedisUtil {
private static Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
@SuppressWarnings("unchecked")
public static RedisTemplate<String, String> redisTemplate = SpringContextHolder.getBean(RedisTemplate.class);
// private static int DEFAULT_EXPIRE_MIN = 1;
public static void set(String key, String value) {
set(key, value, Const.DEFAULT_EXPIRE_TIME);
}
public static void setFalsenum(String key,String value) {
set(key,value,Const.DEFAULT_TOKEN_EXPIRE_TIME);
}
public static void set(String key, String value, int expire_min) {
redisTemplate.opsForValue().set(key, value, expire_min, TimeUnit.SECONDS);
}
public static void setObject(String key, Object value) {
setObject(key, value, Const.DEFAULT_EXPIRE_TIME);
}
public static void setLockValue(String key, Object value) {
String str = JsonUtil.parse(value);
redisTemplate.opsForValue().set(key, str,Const.DEFAULT_TOKEN_EXPIRE_TIME,TimeUnit.SECONDS);
}
public static void setObject(String key, Object value, int expire_min) {
String str = JsonUtil.parse(value);
redisTemplate.opsForValue().set(key, str, expire_min, TimeUnit.SECONDS);
}
public static void delete(String key) {
redisTemplate.delete(key);
}
public static String get(String key) {
return redisTemplate.opsForValue().get(key);
}
public static void deleteKeys(String key) {
Set<String> keys = redisTemplate.keys(key);
redisTemplate.delete(keys);
}
public static User getMiniappsUser(String token) {
if (token == null) {
return null;
}
User user = RedisUtil.getObject(Const.MINIAPPS_CUSTOMER_TOKEN + token, User.class);
// 检查过期时间
if (user != null) {
long expireTime = user.getExpireTime().getTime() - System.currentTimeMillis();
if (expireTime < Const.DEFAULT_TOKEN_REFRESH_TIME) {
redisTemplate.expire(token, Const.DEFAULT_TOKEN_EXPIRE_TIME, TimeUnit.SECONDS);
}
}
return user;
}
public static void setCustomerUser(String token, User user) {
if (user == null) {
throw new BusinessException(Result.authFail("请重新登录"));
}
user.setPassword(null);
user.setExpireTime(new Date(System.currentTimeMillis() + Const.DEFAULT_TOKEN_EXPIRE_TIME));
RedisUtil.setObject(Const.MINIAPPS_CUSTOMER_TOKEN + token, user, Const.DEFAULT_TOKEN_EXPIRE_TIME);
}
public static <T> T getObject(String key, Class<T> clazz) {
String str = redisTemplate.opsForValue().get(key);
if (str != null) {
T res = null;
Gson gson = new Gson();
res = gson.fromJson(str, clazz);
return res;
}
return null;
}
public static <T> List<T> getList(String key, Class<T> clazz){
String str = redisTemplate.opsForValue().get(key);
if (str != null) {
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<T>>() {}.getType();
ArrayList<T> sList=gson.fromJson(str, type);
return sList;
}
return null;
}
/*
* 判断是否存在
*/
public static boolean hasKey(String key){
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/*
* 获取过期时间
*/
public static long getExpire(String key){
return redisTemplate.getExpire(key,TimeUnit.SECONDS);
}
}
使用Redis作缓存数据库,将对像转换成json字符串后存入redis数据库,然后取出相应的值,取出值后再将其转化成对像,使用的是gson类的fromJson(str, clazz)方法,这个方法只能将字符串转化成对像,不能将字符串转化成数组,如果字符串是一个json数组的话就会出错是,于是就要使用gson的fromJson(str, type)方法,就能正确转换。 image.png
网友评论