增加菜单的定时刷新

This commit is contained in:
YunaiV 2021-01-22 20:37:51 +08:00
parent 7bd81d0312
commit 79a0b4cb56
8 changed files with 76 additions and 19 deletions

View File

@ -28,13 +28,6 @@
</exclusion>
</exclusions>
</dependency>
<!-- 通用工具-->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -23,6 +23,6 @@ public interface ConfigFrameworkDAO {
*
* @return 配置列表
*/
List<InfConfigDO> getSysConfigList();
List<InfConfigDO> selectList();
}

View File

@ -73,7 +73,6 @@ public class DBConfigRepository extends AbstractConfigRepository {
if (CollUtil.isEmpty(configs)) { // 如果没有更新则返回
return;
}
log.info("[sync][同步到新配置,配置数量为:{}]", configs.size());
// 第二步构建新的 Properties
Properties newProperties = this.buildProperties(configs);
@ -83,6 +82,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
this.maxUpdateTime = configs.stream().max(Comparator.comparing(BaseDO::getUpdateTime)).get().getUpdateTime();
// 第四部触发配置刷新重要
super.fireRepositoryChange(m_namespace, newProperties);
log.info("[sync][缓存配置,数量为:{}]", configs.size());
}
@Override
@ -150,7 +150,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
log.info("[loadConfigIfUpdate][增量加载全量配置]");
}
// 第二步如果有更新则从数据库加载所有配置
return configFrameworkDAO.getSysConfigList();
return configFrameworkDAO.selectList();
}
}

View File

@ -0,0 +1,9 @@
package cn.iocoder.dashboard.framework.quartz.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling // 开启 Spring 自带的定时任务
public class QuartzConfig {
}

View File

@ -0,0 +1,5 @@
/**
* 定时任务采用 Quartz 实现进程内的任务执行
* 考虑到高可用使用 Quartz 自带的 MySQL 集群方案
*/
package cn.iocoder.dashboard.framework.quartz;

View File

@ -32,7 +32,7 @@ public class InfConfigDAOImpl implements ConfigFrameworkDAO {
}
@Override
public List<InfConfigDO> getSysConfigList() {
public List<InfConfigDO> selectList() {
return jdbcTemplate.query("SELECT `key`, `value`, update_time, deleted FROM inf_config", new BeanPropertyRowMapper<>(InfConfigDO.class));
}

View File

@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.Date;
import java.util.List;
@Mapper
@ -30,4 +31,9 @@ public interface SysMenuMapper extends BaseMapper<SysMenuDO> {
return selectList(new QueryWrapper<>());
}
default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
return selectOne(new QueryWrapper<SysMenuDO>().select("id")
.gt("update_time", maxUpdateTime).last("LIMIT 1")) != null;
}
}

View File

@ -1,6 +1,8 @@
package cn.iocoder.dashboard.modules.system.service.permission.impl;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuListReqVO;
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO;
@ -16,14 +18,12 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
@ -37,6 +37,12 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
@Slf4j
public class SysMenuServiceImpl implements SysMenuService {
/**
* 定时执行 {@link #schedulePeriodicRefresh()} 的周期
* 因为已经通过 Redis Pub/Sub 机制所以频率不需要高
*/
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
/**
* 菜单缓存
* key菜单编号
@ -52,6 +58,10 @@ public class SysMenuServiceImpl implements SysMenuService {
* 这里声明 volatile 修饰的原因是每次刷新时直接修改指向
*/
private volatile Multimap<String, SysMenuDO> permMenuCache;
/**
* 缓存菜单的最大更新时间用于后续的增量轮询判断是否有更新
*/
private volatile Date maxUpdateTime;
@Resource
private SysMenuMapper menuMapper;
@ -63,8 +73,14 @@ public class SysMenuServiceImpl implements SysMenuService {
*/
@Override
@PostConstruct
public void init() {
List<SysMenuDO> menuList = menuMapper.selectList();
public synchronized void init() {
// 获取
List<SysMenuDO> menuList = this.loadMenuIfUpdate(maxUpdateTime);
if (CollUtil.isEmpty(menuList)) {
return;
}
// 构建缓存
ImmutableMap.Builder<Long, SysMenuDO> menuCacheBuilder = ImmutableMap.builder();
ImmutableMultimap.Builder<String, SysMenuDO> permMenuCacheBuilder = ImmutableMultimap.builder();
menuList.forEach(menuDO -> {
@ -73,7 +89,35 @@ public class SysMenuServiceImpl implements SysMenuService {
});
menuCache = menuCacheBuilder.build();
permMenuCache = permMenuCacheBuilder.build();
log.info("[init][初始化菜单数量为 {}]", menuList.size());
assert menuList.size() > 0; // 断言避免告警
maxUpdateTime = menuList.stream().max(Comparator.comparing(BaseDO::getUpdateTime)).get().getUpdateTime();
log.info("[init][缓存菜单,数量为:{}]", menuList.size());
}
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
public void schedulePeriodicRefresh() {
init();
}
/**
* 如果菜单发生变化从数据库中获取最新的全量菜单
* 如果未发生变化则返回空
*
* @param maxUpdateTime 当前菜单的最大更新时间
* @return 菜单列表
*/
private List<SysMenuDO> loadMenuIfUpdate(Date maxUpdateTime) {
// 第一步判断是否要更新
if (maxUpdateTime == null) { // 如果更新时间为空说明 DB 一定有新数据
log.info("[loadMenuIfUpdate][首次加载全量菜单]");
} else { // 判断数据库中是否有更新的菜单
if (!menuMapper.selectExistsByUpdateTimeAfter(maxUpdateTime)) {
return null;
}
log.info("[loadMenuIfUpdate][增量加载全量菜单]");
}
// 第二步如果有更新则从数据库加载所有菜单
return menuMapper.selectList();
}
@Override