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() {
log.info("初始化枚举字典数据----");
//在包下扫描出BaseEnum的子类
Set<Class<?>> 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<Field> extDataFields = Arrays.stream(fields)
//子类是否重写了父类的serializer方法
boolean isRewriteSerializer = !superSerializer.equals(ClassUtil.getDeclaredMethod(aClass, "serializer"));
// 获取所有字段并过滤出非静态字段并带有 IsExtData 注解的字段
List<Field> 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<Object> vo = new SelectNodeVo<>();

View File

@ -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"],
}