增加菜单的定时刷新

pull/2/head
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