Pārlūkot izejas kodu

redis缓存字典信息

rengb 3 gadi atpakaļ
vecāks
revīzija
2c49b5703a

+ 5 - 0
common/src/main/java/com/lantone/common/constant/AuthConstant.java

@@ -32,6 +32,11 @@ public interface AuthConstant {
      */
     String RESOURCE_ROLES_MAP_KEY = "auth:resourceRolesMap";
 
+    /**
+     * Redis缓存字典信息
+     */
+    String DIC_MAP_KEY = "dicMap";
+
     /**
      * 认证信息Http请求头
      */

+ 37 - 0
dblayer-mbg/src/main/java/com/lantone/dblayermbg/facade/DictionaryInfoFacade.java

@@ -1,8 +1,18 @@
 package com.lantone.dblayermbg.facade;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.lantone.common.constant.AuthConstant;
+import com.lantone.common.enums.IsDeleteEnum;
+import com.lantone.common.enums.StatusEnum;
+import com.lantone.common.service.RedisService;
+import com.lantone.dblayermbg.entity.DictionaryInfo;
 import com.lantone.dblayermbg.service.impl.DictionaryInfoServiceImpl;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.Map;
+import java.util.stream.Collectors;
+
 /**
  * <p>
  * 表名:sys_dictionary_info 业务类
@@ -11,4 +21,31 @@ import org.springframework.stereotype.Component;
 @Component
 public class DictionaryInfoFacade extends DictionaryInfoServiceImpl {
 
+    @Autowired
+    private RedisService redisService;
+
+    public Map<Integer, Map<Long, Map<String, String>>> initDic() {
+        QueryWrapper<DictionaryInfo> dictionaryInfoQueryWrapper = new QueryWrapper<DictionaryInfo>()
+                .eq("is_deleted", IsDeleteEnum.N.getKey())
+                .eq("status", StatusEnum.Enable.getKey())
+                .orderByDesc("order_no");
+        Map<Integer, Map<Long, Map<String, String>>> dicMap = list(dictionaryInfoQueryWrapper)
+                .stream()
+                .collect(
+                        Collectors.groupingBy(
+                                DictionaryInfo::getReturnType,
+                                Collectors.groupingBy(
+                                        DictionaryInfo::getGroupType,
+                                        Collectors.toMap(
+                                                DictionaryInfo::getName,
+                                                DictionaryInfo::getVal
+                                        )
+                                )
+                        )
+                );
+        redisService.del(AuthConstant.DIC_MAP_KEY);
+        redisService.set(AuthConstant.DIC_MAP_KEY, dicMap);
+        return dicMap;
+    }
+
 }

+ 28 - 0
security-center/src/main/java/com/lantone/security/component/DictionaryHolder.java

@@ -0,0 +1,28 @@
+package com.lantone.security.component;
+
+import com.lantone.dblayermbg.facade.DictionaryInfoFacade;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * @Description: 字典信息操作组件
+ * @author: rengb
+ * @time: 2021/1/5 18:27
+ */
+@Component
+public class DictionaryHolder {
+
+    @Autowired
+    private DictionaryInfoFacade dictionaryInfoFacade;
+
+    /**
+     * 预加载所有字典信息
+     */
+    @PostConstruct
+    public void initResourceRolesMap() {
+        dictionaryInfoFacade.initDic();
+    }
+
+}