refactor(cache): 优化枚举数据初始化和序列化逻辑

- 优化枚举字典数据初始化流程
- 添加判断子类是否重写父类序列化方法的逻辑
- 调整代码结构,提高可读性和性能
This commit is contained in:
luozhun 2024-11-15 17:31:07 +08:00
parent 60c0f62aed
commit a4c425d190
2 changed files with 8 additions and 7 deletions

View File

@ -36,18 +36,19 @@ public class GlobalCacheManager {
static void initEnum() { static void initEnum() {
log.info("初始化枚举字典数据----"); log.info("初始化枚举字典数据----");
//在包下扫描出BaseEnum的子类
Set<Class<?>> classes = ClassUtil.scanPackageBySuper("com.changhu.common.db.enums", BaseEnum.class); Set<Class<?>> classes = ClassUtil.scanPackageBySuper("com.changhu.common.db.enums", BaseEnum.class);
//序列化方法
Method superSerializer = ClassUtil.getDeclaredMethod(BaseEnum.class, "serializer"); Method superSerializer = ClassUtil.getDeclaredMethod(BaseEnum.class, "serializer");
for (Class<?> aClass : classes) { for (Class<?> aClass : classes) {
Method childrenSerializer = ClassUtil.getDeclaredMethod(aClass, "serializer");
BaseEnum<?>[] enumConstants = (BaseEnum<?>[]) aClass.getEnumConstants(); BaseEnum<?>[] enumConstants = (BaseEnum<?>[]) aClass.getEnumConstants();
if (enumConstants == null) { if (enumConstants == null) {
continue; continue;
} }
// 获取所有字段 //子类是否重写了父类的serializer方法
Field[] fields = aClass.getDeclaredFields(); boolean isRewriteSerializer = !superSerializer.equals(ClassUtil.getDeclaredMethod(aClass, "serializer"));
// 过滤出非静态字段并带有 IsExtData 注解的字段 // 获取所有字段并过滤出非静态字段并带有 IsExtData 注解的字段
List<Field> extDataFields = Arrays.stream(fields) List<Field> extDataFields = Arrays.stream(aClass.getDeclaredFields())
.filter(field -> !Modifier.isStatic(field.getModifiers())) .filter(field -> !Modifier.isStatic(field.getModifiers()))
.filter(field -> field.getAnnotation(IsExtData.class) != null) .filter(field -> field.getAnnotation(IsExtData.class) != null)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -56,7 +57,7 @@ public class GlobalCacheManager {
Stream.of(enumConstants).forEach(v -> { Stream.of(enumConstants).forEach(v -> {
//如果子类重写父类的方法 则直接使用子类的结果 //如果子类重写父类的方法 则直接使用子类的结果
if (!superSerializer.equals(childrenSerializer)) { if (isRewriteSerializer) {
map.put(v, v.serializer()); map.put(v, v.serializer());
} else { } else {
SelectNodeVo<Object> vo = new SelectNodeVo<>(); SelectNodeVo<Object> vo = new SelectNodeVo<>();

View File

@ -20,7 +20,7 @@ export const useUserStore = defineStore({
getTokenInfo: (state): TokenInfo => state.tokenInfo as TokenInfo, getTokenInfo: (state): TokenInfo => state.tokenInfo as TokenInfo,
}, },
persist: { persist: {
key: "useUserStore", key: "spUserStore",
storage: window.localStorage, storage: window.localStorage,
paths: ["tokenInfo"], paths: ["tokenInfo"],
} }