Commit d352bc1b by zhangxingmin

push

parent bbd61250
......@@ -28,6 +28,7 @@ public enum ResultCode {
FORBIDDEN_ERROR(40300, "禁止访问",""),
SENSITIVE_WORDS_EXIST(50001, "存在禁用类型敏感词",""),
SENSITIVE_TZ_WORDS_EXIST(50002, "存在通知类型敏感词",""),
LINK_INVALID(50003, "访问链接失效",""),
;
//返回码
......
package com.yd.common.utils;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
......@@ -455,4 +456,28 @@ public class RedisUtil {
Long releaseStatus = (Long)this.redisTemplate.execute(redisScript, Collections.singletonList(key),value);
return releaseStatus;
}
/**
* 获取缓存对象,并自动反序列化为指定类型
* (解决 StringRedisSerializer 存储对象为 JSON 字符串时的类型转换问题)
* @param key 缓存键
* @param clazz 目标类型
* @return 反序列化后的对象,若 key 不存在则返回 null
*/
public <T> T getCacheObject(final String key, Class<T> clazz) {
Object value = redisTemplate.opsForValue().get(key);
if (value == null) {
return null;
}
// 如果已经是目标类型,直接返回
if (clazz.isAssignableFrom(value.getClass())) {
return clazz.cast(value);
}
// 如果是 JSON 字符串,用 fastjson2 反序列化(项目已引入)
if (value instanceof String) {
return JSONObject.parseObject((String) value, clazz);
}
// 其他情况(如 LinkedHashMap)也转为 JSON 再反序列化
return JSONObject.parseObject(JSONObject.toJSONString(value), clazz);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment