Commit ffab0b1d6a3673aad4c8de2a099e3cdaad9cfac2

Authored by YunaiV
1 parent 03c7e600

1. 优化 menu 本地缓存的刷新策略,在修改或删除菜单时,强制刷新缓存

2. 修复 pub/sub 未初始化的问题
3. 优化 Job 定时的 mybatis 日志输出,避免一直打印
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 { @@ -53,7 +53,6 @@ public class YudaoMQAutoConfiguration {
53 * 创建 Redis Pub/Sub 广播消费的容器 53 * 创建 Redis Pub/Sub 广播消费的容器
54 */ 54 */
55 @Bean 55 @Bean
56 - @Async // 异步化,可降低 2 秒左右的启动时间  
57 public RedisMessageListenerContainer redisMessageListenerContainer( 56 public RedisMessageListenerContainer redisMessageListenerContainer(
58 RedisMQTemplate redisMQTemplate, List<AbstractChannelMessageListener<?>> listeners) { 57 RedisMQTemplate redisMQTemplate, List<AbstractChannelMessageListener<?>> listeners) {
59 // 创建 RedisMessageListenerContainer 对象 58 // 创建 RedisMessageListenerContainer 对象
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 { @@ -86,11 +86,30 @@ public class MenuServiceImpl implements MenuService {
86 @Override 86 @Override
87 @PostConstruct 87 @PostConstruct
88 public synchronized void initLocalCache() { 88 public synchronized void initLocalCache() {
89 - // 获取菜单列表,如果有更新  
90 - List<MenuDO> menuList = this.loadMenuIfUpdate(maxUpdateTime);  
91 - if (CollUtil.isEmpty(menuList)) { 89 + initLocalCacheIfUpdate(null);
  90 + }
  91 +
  92 + @Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
  93 + public void schedulePeriodicRefresh() {
  94 + initLocalCacheIfUpdate(this.maxUpdateTime);
  95 + }
  96 +
  97 + /**
  98 + * 刷新本地缓存
  99 + *
  100 + * @param maxUpdateTime 最大更新时间
  101 + * 1. 如果 maxUpdateTime 为 null,则“强制”刷新缓存
  102 + * 2. 如果 maxUpdateTime 不为 null,判断自 maxUpdateTime 是否有数据发生变化,有的情况下才刷新缓存
  103 + */
  104 + private void initLocalCacheIfUpdate(LocalDateTime maxUpdateTime) {
  105 + // 如果没有增量的数据变化,则不进行本地缓存的刷新
  106 + if (maxUpdateTime != null
  107 + && menuMapper.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {
  108 + log.info("[initLocalCacheIfUpdate][数据未发生变化({}),本地缓存不刷新]", maxUpdateTime);
92 return; 109 return;
93 } 110 }
  111 + List<MenuDO> menuList = menuMapper.selectList();
  112 + log.info("[initLocalCacheIfUpdate][缓存菜单,数量为:{}]", menuList.size());
94 113
95 // 构建缓存 114 // 构建缓存
96 ImmutableMap.Builder<Long, MenuDO> menuCacheBuilder = ImmutableMap.builder(); 115 ImmutableMap.Builder<Long, MenuDO> menuCacheBuilder = ImmutableMap.builder();
@@ -103,34 +122,9 @@ public class MenuServiceImpl implements MenuService { @@ -103,34 +122,9 @@ public class MenuServiceImpl implements MenuService {
103 }); 122 });
104 menuCache = menuCacheBuilder.build(); 123 menuCache = menuCacheBuilder.build();
105 permissionMenuCache = permMenuCacheBuilder.build(); 124 permissionMenuCache = permMenuCacheBuilder.build();
106 - maxUpdateTime = CollectionUtils.getMaxValue(menuList, MenuDO::getUpdateTime);  
107 - log.info("[initLocalCache][缓存菜单,数量为:{}]", menuList.size());  
108 - }  
109 125
110 - @Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)  
111 - public void schedulePeriodicRefresh() {  
112 - initLocalCache();  
113 - }  
114 -  
115 - /**  
116 - * 如果菜单发生变化,从数据库中获取最新的全量菜单。  
117 - * 如果未发生变化,则返回空  
118 - *  
119 - * @param maxUpdateTime 当前菜单的最大更新时间  
120 - * @return 菜单列表  
121 - */  
122 - private List<MenuDO> loadMenuIfUpdate(LocalDateTime maxUpdateTime) {  
123 - // 第一步,判断是否要更新。  
124 - if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据  
125 - log.info("[loadMenuIfUpdate][首次加载全量菜单]");  
126 - } else { // 判断数据库中是否有更新的菜单  
127 - if (menuMapper.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {  
128 - return null;  
129 - }  
130 - log.info("[loadMenuIfUpdate][增量加载全量菜单]");  
131 - }  
132 - // 第二步,如果有更新,则从数据库加载所有菜单  
133 - return menuMapper.selectList(); 126 + // 设置最新的 maxUpdateTime,用于下次的增量判断
  127 + this.maxUpdateTime = CollectionUtils.getMaxValue(menuList, MenuDO::getUpdateTime);
134 } 128 }
135 129
136 @Override 130 @Override
yudao-server/src/main/java/cn/iocoder/yudao/module/shop/controller/app/AppShopOrderController.java
1 package cn.iocoder.yudao.module.shop.controller.app; 1 package cn.iocoder.yudao.module.shop.controller.app;
2 2
3 -import cn.hutool.core.date.LocalDateTimeUtil;  
4 import cn.iocoder.yudao.framework.common.pojo.CommonResult; 3 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
5 import cn.iocoder.yudao.module.pay.service.notify.vo.PayNotifyOrderReqVO; 4 import cn.iocoder.yudao.module.pay.service.notify.vo.PayNotifyOrderReqVO;
6 import cn.iocoder.yudao.module.pay.service.notify.vo.PayRefundOrderReqVO; 5 import cn.iocoder.yudao.module.pay.service.notify.vo.PayRefundOrderReqVO;
@@ -20,7 +19,6 @@ import org.springframework.web.bind.annotation.RestController; @@ -20,7 +19,6 @@ import org.springframework.web.bind.annotation.RestController;
20 import javax.annotation.Resource; 19 import javax.annotation.Resource;
21 import javax.validation.Valid; 20 import javax.validation.Valid;
22 import java.time.LocalDateTime; 21 import java.time.LocalDateTime;
23 -import java.time.temporal.ChronoUnit;  
24 22
25 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; 23 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
26 import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; 24 import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
yudao-server/src/main/resources/application-local.yaml
@@ -151,12 +151,14 @@ logging: @@ -151,12 +151,14 @@ logging:
151 # 配置自己写的 MyBatis Mapper 打印日志 151 # 配置自己写的 MyBatis Mapper 打印日志
152 cn.iocoder.yudao.module.bpm.dal.mysql: debug 152 cn.iocoder.yudao.module.bpm.dal.mysql: debug
153 cn.iocoder.yudao.module.infra.dal.mysql: debug 153 cn.iocoder.yudao.module.infra.dal.mysql: debug
  154 + cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper: INFO # 配置 JobLogMapper 的日志级别为 info
154 cn.iocoder.yudao.module.pay.dal.mysql: debug 155 cn.iocoder.yudao.module.pay.dal.mysql: debug
155 cn.iocoder.yudao.module.system.dal.mysql: debug 156 cn.iocoder.yudao.module.system.dal.mysql: debug
156 cn.iocoder.yudao.module.tool.dal.mysql: debug 157 cn.iocoder.yudao.module.tool.dal.mysql: debug
157 cn.iocoder.yudao.module.member.dal.mysql: debug 158 cn.iocoder.yudao.module.member.dal.mysql: debug
158 cn.iocoder.yudao.module.trade.dal.mysql: debug 159 cn.iocoder.yudao.module.trade.dal.mysql: debug
159 cn.iocoder.yudao.module.promotion.dal.mysql: debug 160 cn.iocoder.yudao.module.promotion.dal.mysql: debug
  161 +
160 debug: false 162 debug: false
161 163
162 --- #################### 微信公众号、小程序相关配置 #################### 164 --- #################### 微信公众号、小程序相关配置 ####################