Browse Source

1. 优化 menu 本地缓存的刷新策略,在修改或删除菜单时,强制刷新缓存
2. 修复 pub/sub 未初始化的问题
3. 优化 Job 定时的 mybatis 日志输出,避免一直打印

YunaiV 2 years ago
parent
commit
ffab0b1d6a

+ 0 - 1
yudao-framework/yudao-spring-boot-starter-mq/src/main/java/cn/iocoder/yudao/framework/mq/config/YudaoMQAutoConfiguration.java

@@ -53,7 +53,6 @@ public class YudaoMQAutoConfiguration {
      * 创建 Redis Pub/Sub 广播消费的容器
      */
     @Bean
-    @Async // 异步化,可降低 2 秒左右的启动时间
     public RedisMessageListenerContainer redisMessageListenerContainer(
             RedisMQTemplate redisMQTemplate, List<AbstractChannelMessageListener<?>> listeners) {
         // 创建 RedisMessageListenerContainer 对象

+ 24 - 30
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/MenuServiceImpl.java

@@ -86,11 +86,30 @@ public class MenuServiceImpl implements MenuService {
     @Override
     @PostConstruct
     public synchronized void initLocalCache() {
-        // 获取菜单列表,如果有更新
-        List<MenuDO> menuList = this.loadMenuIfUpdate(maxUpdateTime);
-        if (CollUtil.isEmpty(menuList)) {
+        initLocalCacheIfUpdate(null);
+    }
+
+    @Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
+    public void schedulePeriodicRefresh() {
+        initLocalCacheIfUpdate(this.maxUpdateTime);
+    }
+
+    /**
+     * 刷新本地缓存
+     *
+     * @param maxUpdateTime 最大更新时间
+     *                      1. 如果 maxUpdateTime 为 null,则“强制”刷新缓存
+     *                      2. 如果 maxUpdateTime 不为 null,判断自 maxUpdateTime 是否有数据发生变化,有的情况下才刷新缓存
+     */
+    private void initLocalCacheIfUpdate(LocalDateTime maxUpdateTime) {
+        // 如果没有增量的数据变化,则不进行本地缓存的刷新
+        if (maxUpdateTime != null
+            && menuMapper.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {
+            log.info("[initLocalCacheIfUpdate][数据未发生变化({}),本地缓存不刷新]", maxUpdateTime);
             return;
         }
+        List<MenuDO> menuList = menuMapper.selectList();
+        log.info("[initLocalCacheIfUpdate][缓存菜单,数量为:{}]", menuList.size());
 
         // 构建缓存
         ImmutableMap.Builder<Long, MenuDO> menuCacheBuilder = ImmutableMap.builder();
@@ -103,34 +122,9 @@ public class MenuServiceImpl implements MenuService {
         });
         menuCache = menuCacheBuilder.build();
         permissionMenuCache = permMenuCacheBuilder.build();
-        maxUpdateTime = CollectionUtils.getMaxValue(menuList, MenuDO::getUpdateTime);
-        log.info("[initLocalCache][缓存菜单,数量为:{}]", menuList.size());
-    }
 
-    @Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
-    public void schedulePeriodicRefresh() {
-        initLocalCache();
-    }
-
-    /**
-     * 如果菜单发生变化,从数据库中获取最新的全量菜单。
-     * 如果未发生变化,则返回空
-     *
-     * @param maxUpdateTime 当前菜单的最大更新时间
-     * @return 菜单列表
-     */
-    private List<MenuDO> loadMenuIfUpdate(LocalDateTime maxUpdateTime) {
-        // 第一步,判断是否要更新。
-        if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据
-            log.info("[loadMenuIfUpdate][首次加载全量菜单]");
-        } else { // 判断数据库中是否有更新的菜单
-            if (menuMapper.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {
-                return null;
-            }
-            log.info("[loadMenuIfUpdate][增量加载全量菜单]");
-        }
-        // 第二步,如果有更新,则从数据库加载所有菜单
-        return menuMapper.selectList();
+        // 设置最新的 maxUpdateTime,用于下次的增量判断
+        this.maxUpdateTime = CollectionUtils.getMaxValue(menuList, MenuDO::getUpdateTime);
     }
 
     @Override

+ 0 - 2
yudao-server/src/main/java/cn/iocoder/yudao/module/shop/controller/app/AppShopOrderController.java

@@ -1,6 +1,5 @@
 package cn.iocoder.yudao.module.shop.controller.app;
 
-import cn.hutool.core.date.LocalDateTimeUtil;
 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 import cn.iocoder.yudao.module.pay.service.notify.vo.PayNotifyOrderReqVO;
 import cn.iocoder.yudao.module.pay.service.notify.vo.PayRefundOrderReqVO;
@@ -20,7 +19,6 @@ import org.springframework.web.bind.annotation.RestController;
 import javax.annotation.Resource;
 import javax.validation.Valid;
 import java.time.LocalDateTime;
-import java.time.temporal.ChronoUnit;
 
 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;

+ 2 - 0
yudao-server/src/main/resources/application-local.yaml

@@ -151,12 +151,14 @@ logging:
     # 配置自己写的 MyBatis Mapper 打印日志
     cn.iocoder.yudao.module.bpm.dal.mysql: debug
     cn.iocoder.yudao.module.infra.dal.mysql: debug
+    cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper: INFO # 配置 JobLogMapper 的日志级别为 info
     cn.iocoder.yudao.module.pay.dal.mysql: debug
     cn.iocoder.yudao.module.system.dal.mysql: debug
     cn.iocoder.yudao.module.tool.dal.mysql: debug
     cn.iocoder.yudao.module.member.dal.mysql: debug
     cn.iocoder.yudao.module.trade.dal.mysql: debug
     cn.iocoder.yudao.module.promotion.dal.mysql: debug
+
 debug: false
 
 --- #################### 微信公众号、小程序相关配置 ####################