redis反序列化问题
This commit is contained in:
@@ -7,6 +7,8 @@ import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
|
||||
/**
|
||||
* @description RedisService.java Redis工具服务类,封装常用Redis操作
|
||||
* @filename RedisService.java
|
||||
@@ -56,6 +58,28 @@ public class RedisService {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 获取key对应的value并反序列化为指定类型
|
||||
* @param key String 键
|
||||
* @param clazz Class<T> 目标类型
|
||||
* @return T 反序列化后的对象
|
||||
* @author yslg
|
||||
* @since 2025-12-19
|
||||
*/
|
||||
public <T> T get(String key, Class<T> clazz) {
|
||||
Object obj = redisTemplate.opsForValue().get(key);
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
if (clazz.isInstance(obj)) {
|
||||
return clazz.cast(obj);
|
||||
}
|
||||
if (obj instanceof String) {
|
||||
return JSON.parseObject((String) obj, clazz);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 删除key
|
||||
* @param key String 键
|
||||
|
||||
Reference in New Issue
Block a user