From a4c425d190d2b797ae32baf92be01c1f049dd88d Mon Sep 17 00:00:00 2001 From: luozhun <2025254074@qq.com> Date: Fri, 15 Nov 2024 17:31:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cache):=20=E4=BC=98=E5=8C=96=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E6=95=B0=E6=8D=AE=E5=88=9D=E5=A7=8B=E5=8C=96=E5=92=8C?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化枚举字典数据初始化流程 - 添加判断子类是否重写父类序列化方法的逻辑 - 调整代码结构,提高可读性和性能 --- .../changhu/common/cache/GlobalCacheManager.java | 13 +++++++------ superManagement/src/stores/modules/userStore.ts | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/policeSecurityServer/src/main/java/com/changhu/common/cache/GlobalCacheManager.java b/policeSecurityServer/src/main/java/com/changhu/common/cache/GlobalCacheManager.java index 6738b5a..7ea767c 100644 --- a/policeSecurityServer/src/main/java/com/changhu/common/cache/GlobalCacheManager.java +++ b/policeSecurityServer/src/main/java/com/changhu/common/cache/GlobalCacheManager.java @@ -36,18 +36,19 @@ public class GlobalCacheManager { static void initEnum() { log.info("初始化枚举字典数据----"); + //在包下扫描出BaseEnum的子类 Set> classes = ClassUtil.scanPackageBySuper("com.changhu.common.db.enums", BaseEnum.class); + //序列化方法 Method superSerializer = ClassUtil.getDeclaredMethod(BaseEnum.class, "serializer"); for (Class aClass : classes) { - Method childrenSerializer = ClassUtil.getDeclaredMethod(aClass, "serializer"); BaseEnum[] enumConstants = (BaseEnum[]) aClass.getEnumConstants(); if (enumConstants == null) { continue; } - // 获取所有字段 - Field[] fields = aClass.getDeclaredFields(); - // 过滤出非静态字段并带有 IsExtData 注解的字段 - List extDataFields = Arrays.stream(fields) + //子类是否重写了父类的serializer方法 + boolean isRewriteSerializer = !superSerializer.equals(ClassUtil.getDeclaredMethod(aClass, "serializer")); + // 获取所有字段并过滤出非静态字段并带有 IsExtData 注解的字段 + List extDataFields = Arrays.stream(aClass.getDeclaredFields()) .filter(field -> !Modifier.isStatic(field.getModifiers())) .filter(field -> field.getAnnotation(IsExtData.class) != null) .collect(Collectors.toList()); @@ -56,7 +57,7 @@ public class GlobalCacheManager { Stream.of(enumConstants).forEach(v -> { //如果子类重写父类的方法 则直接使用子类的结果 - if (!superSerializer.equals(childrenSerializer)) { + if (isRewriteSerializer) { map.put(v, v.serializer()); } else { SelectNodeVo vo = new SelectNodeVo<>(); diff --git a/superManagement/src/stores/modules/userStore.ts b/superManagement/src/stores/modules/userStore.ts index f52f0f5..63cb48e 100644 --- a/superManagement/src/stores/modules/userStore.ts +++ b/superManagement/src/stores/modules/userStore.ts @@ -20,7 +20,7 @@ export const useUserStore = defineStore({ getTokenInfo: (state): TokenInfo => state.tokenInfo as TokenInfo, }, persist: { - key: "useUserStore", + key: "spUserStore", storage: window.localStorage, paths: ["tokenInfo"], }