fix(#758): guanyu (文件合入)

This commit is contained in:
2026-06-13 12:45:55 +08:00
committed by 华佗
parent 77e75df0c0
commit edfcccba24
3 changed files with 133 additions and 96 deletions

View File

@@ -1,13 +1,14 @@
package com.core.common.utils;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson2.JSONArray;
import com.core.common.constant.CacheConstants;
import com.core.common.core.domain.entity.SysDictData;
import com.core.common.core.redis.RedisCache;
import com.core.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.List;
@@ -17,6 +18,7 @@ import java.util.List;
* @author system
*/
public class DictUtils {
private static final Logger log = LoggerFactory.getLogger(DictUtils.class);
/**
* 分隔符
*/
@@ -39,24 +41,39 @@ public class DictUtils {
* @return dictDatas 字典数据列表
*/
public static List<SysDictData> getDictCache(String key) {
Object cached = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNull(cached)) {
try {
Object cached = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
if (cached == null) {
return null;
}
if (cached instanceof JSONArray arrayCache) {
return arrayCache.toList(SysDictData.class);
}
if (cached instanceof List<?> list) {
// Redis缓存可能已反序列化为List尝试逐个转换
java.util.ArrayList<SysDictData> result = new java.util.ArrayList<>(list.size());
for (Object item : list) {
if (item instanceof SysDictData dictData) {
result.add(dictData);
} else {
// 尝试通过JSON转换
String json = com.alibaba.fastjson2.JSON.toJSONString(item);
result.add(com.alibaba.fastjson2.JSON.parseObject(json, SysDictData.class));
}
}
return result;
}
log.warn("字典缓存key={}的数据类型异常: {}, 清除缓存", key, cached.getClass().getName());
removeDictCache(key);
return null;
} catch (Exception e) {
log.warn("获取字典缓存异常key={}, 清除缓存: {}", key, e.getMessage());
try {
removeDictCache(key);
} catch (Exception ignored) {
}
return null;
}
// 如果已经是目标类型,直接返回
if (cached instanceof List && ((List<?>) cached).stream().allMatch(e -> e instanceof SysDictData)) {
@SuppressWarnings("unchecked")
List<SysDictData> result = (List<SysDictData>) cached;
return result;
}
com.fasterxml.jackson.core.type.TypeReference<List<SysDictData>> typeRef =
new com.fasterxml.jackson.core.type.TypeReference<List<SysDictData>>() {};
ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (cached instanceof JsonNode jsonNode) {
return mapper.convertValue(jsonNode, typeRef);
}
return mapper.convertValue(cached, typeRef);
}
/**