parent
c10ab1753a
commit
dc42f0f1bb
|
@ -1,6 +1,6 @@
|
||||||
package cn.iocoder.dashboard.framework.redis.config;
|
package cn.iocoder.dashboard.framework.redis.config;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.framework.redis.core.listener.AbstractMessageListener;
|
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
|
||||||
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
|
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -35,7 +35,7 @@ public class RedisConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory,
|
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory,
|
||||||
List<AbstractMessageListener<?>> listeners) {
|
List<AbstractChannelMessageListener<?>> listeners) {
|
||||||
// 创建 RedisMessageListenerContainer 对象
|
// 创建 RedisMessageListenerContainer 对象
|
||||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||||
// 设置 RedisConnection 工厂。
|
// 设置 RedisConnection 工厂。
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package cn.iocoder.dashboard.framework.redis.core.listener;
|
package cn.iocoder.dashboard.framework.redis.core.pubsub;
|
||||||
|
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.iocoder.dashboard.util.json.JSONUtils;
|
import cn.iocoder.dashboard.util.json.JSONUtils;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
import org.springframework.data.redis.connection.Message;
|
import org.springframework.data.redis.connection.Message;
|
||||||
import org.springframework.data.redis.connection.MessageListener;
|
import org.springframework.data.redis.connection.MessageListener;
|
||||||
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
|
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
|
||||||
|
@ -15,15 +16,21 @@ import java.lang.reflect.Type;
|
||||||
*
|
*
|
||||||
* @author 芋道源码
|
* @author 芋道源码
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractMessageListener<T> implements MessageListener {
|
public abstract class AbstractChannelMessageListener<T extends ChannelMessage> implements MessageListener {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 消息类型
|
* 消息类型
|
||||||
*/
|
*/
|
||||||
private final Class<T> messageType;
|
private final Class<T> messageType;
|
||||||
|
/**
|
||||||
|
* Redis Channel
|
||||||
|
*/
|
||||||
|
private final String channel;
|
||||||
|
|
||||||
protected AbstractMessageListener() {
|
@SneakyThrows
|
||||||
|
protected AbstractChannelMessageListener() {
|
||||||
this.messageType = getMessageClass();
|
this.messageType = getMessageClass();
|
||||||
|
this.channel = messageType.newInstance().getChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,7 +38,9 @@ public abstract class AbstractMessageListener<T> implements MessageListener {
|
||||||
*
|
*
|
||||||
* @return channel
|
* @return channel
|
||||||
*/
|
*/
|
||||||
public abstract String getChannel();
|
public final String getChannel() {
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void onMessage(Message message, byte[] bytes) {
|
public final void onMessage(Message message, byte[] bytes) {
|
||||||
|
@ -56,7 +65,7 @@ public abstract class AbstractMessageListener<T> implements MessageListener {
|
||||||
Class<?> targetClass = getClass();
|
Class<?> targetClass = getClass();
|
||||||
while (targetClass.getSuperclass() != null) {
|
while (targetClass.getSuperclass() != null) {
|
||||||
// 如果不是 AbstractMessageListener 父类,继续向上查找
|
// 如果不是 AbstractMessageListener 父类,继续向上查找
|
||||||
if (targetClass.getSuperclass() != AbstractMessageListener.class) {
|
if (targetClass.getSuperclass() != AbstractChannelMessageListener.class) {
|
||||||
targetClass = targetClass.getSuperclass();
|
targetClass = targetClass.getSuperclass();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package cn.iocoder.dashboard.framework.redis.core.pubsub;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redis Channel Message 接口
|
||||||
|
*/
|
||||||
|
public interface ChannelMessage {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得 Redis Channel
|
||||||
|
*
|
||||||
|
* @return Channel
|
||||||
|
*/
|
||||||
|
@JSONField(serialize = false) // 必须序列化
|
||||||
|
String getChannel();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package cn.iocoder.dashboard.framework.redis.core.util;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
|
||||||
|
import cn.iocoder.dashboard.util.json.JSONUtils;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redis 消息工具类
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
public class RedisMessageUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送 Redis 消息,基于 Redis pub/sub 实现
|
||||||
|
*
|
||||||
|
* @param redisTemplate Redis 操作模板
|
||||||
|
* @param message 消息
|
||||||
|
*/
|
||||||
|
public static <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T message) {
|
||||||
|
redisTemplate.convertAndSend(message.getChannel(), JSONUtils.toJSONString(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -63,7 +63,8 @@ public class SysAuthController {
|
||||||
// 获得角色列表
|
// 获得角色列表
|
||||||
List<SysRoleDO> roleList = roleService.listRolesFromCache(getLoginUserRoleIds());
|
List<SysRoleDO> roleList = roleService.listRolesFromCache(getLoginUserRoleIds());
|
||||||
// 获得菜单列表
|
// 获得菜单列表
|
||||||
List<SysMenuDO> menuList = permissionService.listRoleMenusFromCache(getLoginUserRoleIds(),
|
List<SysMenuDO> menuList = permissionService.listRoleMenusFromCache(
|
||||||
|
getLoginUserRoleIds(), // 注意,基于登陆的角色,因为后续的权限判断也是基于它
|
||||||
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()),
|
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()),
|
||||||
SetUtils.asSet(CommonStatusEnum.ENABLE.getStatus()));
|
SetUtils.asSet(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
// 拼接结果返回
|
// 拼接结果返回
|
||||||
|
@ -74,7 +75,8 @@ public class SysAuthController {
|
||||||
@GetMapping("list-menus")
|
@GetMapping("list-menus")
|
||||||
public CommonResult<List<SysAuthMenuRespVO>> listMenus() {
|
public CommonResult<List<SysAuthMenuRespVO>> listMenus() {
|
||||||
// 获得用户拥有的菜单列表
|
// 获得用户拥有的菜单列表
|
||||||
List<SysMenuDO> menuList = permissionService.listRoleMenusFromCache(getLoginUserRoleIds(),
|
List<SysMenuDO> menuList = permissionService.listRoleMenusFromCache(
|
||||||
|
getLoginUserRoleIds(), // 注意,基于登陆的角色,因为后续的权限判断也是基于它
|
||||||
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType()), // 只要目录和菜单类型
|
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType()), // 只要目录和菜单类型
|
||||||
SetUtils.asSet(CommonStatusEnum.ENABLE.getStatus())); // 只要开启的
|
SetUtils.asSet(CommonStatusEnum.ENABLE.getStatus())); // 只要开启的
|
||||||
// 转换成 Tree 结构返回
|
// 转换成 Tree 结构返回
|
||||||
|
|
|
@ -10,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static com.baomidou.mybatisplus.core.metadata.OrderItem.asc;
|
import static com.baomidou.mybatisplus.core.metadata.OrderItem.asc;
|
||||||
|
@ -42,4 +43,10 @@ public interface SysDictDataMapper extends BaseMapper<SysDictDataDO> {
|
||||||
.likeIfPresent("dict_type", reqVO.getDictType())
|
.likeIfPresent("dict_type", reqVO.getDictType())
|
||||||
.eqIfPresent("status", reqVO.getStatus()));
|
.eqIfPresent("status", reqVO.getStatus()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
|
||||||
|
return selectOne(new QueryWrapper<SysDictDataDO>().select("id")
|
||||||
|
.gt("update_time", maxUpdateTime).last("LIMIT 1")) != null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package cn.iocoder.dashboard.modules.system.mq.consumer.dict;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
|
||||||
|
import cn.iocoder.dashboard.modules.system.mq.message.dict.SysDictDataRefreshMessage;
|
||||||
|
import cn.iocoder.dashboard.modules.system.service.dict.SysDictDataService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对 {@link SysDictDataRefreshMessage} 的消费者
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class SysDictDataRefreshConsumer extends AbstractChannelMessageListener<SysDictDataRefreshMessage> {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SysDictDataService dictDataService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(SysDictDataRefreshMessage message) {
|
||||||
|
log.info("[onMessage][收到 DictData 刷新消息]");
|
||||||
|
dictDataService.initLocalCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package cn.iocoder.dashboard.modules.system.mq.consumer;
|
package cn.iocoder.dashboard.modules.system.mq.consumer.permission;
|
||||||
|
|
||||||
import cn.iocoder.dashboard.framework.redis.core.listener.AbstractMessageListener;
|
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage;
|
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage;
|
||||||
import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService;
|
import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -8,9 +8,14 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对 {@link SysMenuRefreshMessage} 的消费者
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SysMenuRefreshConsumer extends AbstractMessageListener<SysMenuRefreshMessage> {
|
public class SysMenuRefreshConsumer extends AbstractChannelMessageListener<SysMenuRefreshMessage> {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysMenuService menuService;
|
private SysMenuService menuService;
|
||||||
|
@ -18,12 +23,7 @@ public class SysMenuRefreshConsumer extends AbstractMessageListener<SysMenuRefre
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(SysMenuRefreshMessage message) {
|
public void onMessage(SysMenuRefreshMessage message) {
|
||||||
log.info("[onMessage][收到 Menu 刷新消息]");
|
log.info("[onMessage][收到 Menu 刷新消息]");
|
||||||
menuService.init();
|
menuService.initLocalCache();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getChannel() {
|
|
||||||
return SysMenuRefreshMessage.TOPIC;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package cn.iocoder.dashboard.modules.system.mq.message.dict;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典数据数据刷新 Message
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class SysDictDataRefreshMessage implements ChannelMessage {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getChannel() {
|
||||||
|
return "system.dict-data.refresh";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
package cn.iocoder.dashboard.modules.system.mq.message;
|
|
|
@ -1,13 +1,17 @@
|
||||||
package cn.iocoder.dashboard.modules.system.mq.message.permission;
|
package cn.iocoder.dashboard.modules.system.mq.message.permission;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.framework.redis.core.pubsub.ChannelMessage;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜单数据刷新 Message
|
* 菜单数据刷新 Message
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class SysMenuRefreshMessage {
|
public class SysMenuRefreshMessage implements ChannelMessage {
|
||||||
|
|
||||||
public static final String TOPIC = "system.menu.refresh";
|
@Override
|
||||||
|
public String getChannel() {
|
||||||
|
return "system.menu.refresh";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package cn.iocoder.dashboard.modules.system.mq.producer.dict;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
|
||||||
|
import cn.iocoder.dashboard.modules.system.mq.message.dict.SysDictDataRefreshMessage;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DictData 字典数据相关消息的 Producer
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SysDictDataProducer {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送 {@link SysDictDataRefreshMessage} 消息
|
||||||
|
*/
|
||||||
|
public void sendMenuRefreshMessage() {
|
||||||
|
SysDictDataRefreshMessage message = new SysDictDataRefreshMessage();
|
||||||
|
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
package cn.iocoder.dashboard.modules.system.mq.producer;
|
|
|
@ -1,7 +1,7 @@
|
||||||
package cn.iocoder.dashboard.modules.system.mq.producer.permission;
|
package cn.iocoder.dashboard.modules.system.mq.producer.permission;
|
||||||
|
|
||||||
|
import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils;
|
||||||
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage;
|
import cn.iocoder.dashboard.modules.system.mq.message.permission.SysMenuRefreshMessage;
|
||||||
import cn.iocoder.dashboard.util.json.JSONUtils;
|
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ public class SysMenuProducer {
|
||||||
*/
|
*/
|
||||||
public void sendMenuRefreshMessage() {
|
public void sendMenuRefreshMessage() {
|
||||||
SysMenuRefreshMessage message = new SysMenuRefreshMessage();
|
SysMenuRefreshMessage message = new SysMenuRefreshMessage();
|
||||||
stringRedisTemplate.convertAndSend(SysMenuRefreshMessage.TOPIC, JSONUtils.toJSONString(message));
|
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,9 +18,9 @@ import java.util.List;
|
||||||
public interface SysDictDataService extends DictDataFrameworkService {
|
public interface SysDictDataService extends DictDataFrameworkService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化,主要是初始化缓存
|
* 初始化字典数据的本地缓存
|
||||||
*/
|
*/
|
||||||
void init();
|
void initLocalCache();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得字典数据列表
|
* 获得字典数据列表
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package cn.iocoder.dashboard.modules.system.service.dict.impl;
|
package cn.iocoder.dashboard.modules.system.service.dict.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
|
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
|
||||||
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
|
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
|
||||||
import cn.iocoder.dashboard.common.pojo.PageResult;
|
import cn.iocoder.dashboard.common.pojo.PageResult;
|
||||||
|
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
|
||||||
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataCreateReqVO;
|
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataCreateReqVO;
|
||||||
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataExportReqVO;
|
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataExportReqVO;
|
||||||
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataPageReqVO;
|
import cn.iocoder.dashboard.modules.system.controller.dict.vo.data.SysDictDataPageReqVO;
|
||||||
|
@ -11,15 +13,19 @@ import cn.iocoder.dashboard.modules.system.convert.dict.SysDictDataConvert;
|
||||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.dict.SysDictDataMapper;
|
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.dict.SysDictDataMapper;
|
||||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dict.SysDictDataDO;
|
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dict.SysDictDataDO;
|
||||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dict.SysDictTypeDO;
|
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dict.SysDictTypeDO;
|
||||||
|
import cn.iocoder.dashboard.modules.system.mq.producer.dict.SysDictDataProducer;
|
||||||
import cn.iocoder.dashboard.modules.system.service.dict.SysDictDataService;
|
import cn.iocoder.dashboard.modules.system.service.dict.SysDictDataService;
|
||||||
import cn.iocoder.dashboard.modules.system.service.dict.SysDictTypeService;
|
import cn.iocoder.dashboard.modules.system.service.dict.SysDictTypeService;
|
||||||
import com.google.common.collect.ImmutableTable;
|
import com.google.common.collect.ImmutableTable;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
|
import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
|
||||||
|
@ -30,12 +36,22 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@Slf4j
|
||||||
public class SysDictDataServiceImpl implements SysDictDataService {
|
public class SysDictDataServiceImpl implements SysDictDataService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排序 dictType > sort
|
||||||
|
*/
|
||||||
private static final Comparator<SysDictDataDO> COMPARATOR_TYPE_AND_SORT = Comparator
|
private static final Comparator<SysDictDataDO> COMPARATOR_TYPE_AND_SORT = Comparator
|
||||||
.comparing(SysDictDataDO::getDictType)
|
.comparing(SysDictDataDO::getDictType)
|
||||||
.thenComparingInt(SysDictDataDO::getSort);
|
.thenComparingInt(SysDictDataDO::getSort);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定时执行 {@link #schedulePeriodicRefresh()} 的周期
|
||||||
|
* 因为已经通过 Redis Pub/Sub 机制,所以频率不需要高
|
||||||
|
*/
|
||||||
|
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典数据缓存,第二个 key 使用 label
|
* 字典数据缓存,第二个 key 使用 label
|
||||||
*
|
*
|
||||||
|
@ -50,6 +66,10 @@ public class SysDictDataServiceImpl implements SysDictDataService {
|
||||||
* key2:字典值 value
|
* key2:字典值 value
|
||||||
*/
|
*/
|
||||||
private ImmutableTable<String, String, SysDictDataDO> valueDictDataCache;
|
private ImmutableTable<String, String, SysDictDataDO> valueDictDataCache;
|
||||||
|
/**
|
||||||
|
* 缓存字典数据的最大更新时间,用于后续的增量轮询,判断是否有更新
|
||||||
|
*/
|
||||||
|
private volatile Date maxUpdateTime;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SysDictTypeService dictTypeService;
|
private SysDictTypeService dictTypeService;
|
||||||
|
@ -57,20 +77,56 @@ public class SysDictDataServiceImpl implements SysDictDataService {
|
||||||
@Resource
|
@Resource
|
||||||
private SysDictDataMapper dictDataMapper;
|
private SysDictDataMapper dictDataMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SysDictDataProducer dictDataProducer;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void initLocalCache() {
|
||||||
// 获得字典数据
|
// 获取字典数据列表,如果有更新
|
||||||
List<SysDictDataDO> list = this.listDictDatas();
|
List<SysDictDataDO> dataList = this.loadDictDataIfUpdate(maxUpdateTime);
|
||||||
|
if (CollUtil.isEmpty(dataList)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 构建缓存
|
// 构建缓存
|
||||||
ImmutableTable.Builder<String, String, SysDictDataDO> labelDictDataBuilder = ImmutableTable.builder();
|
ImmutableTable.Builder<String, String, SysDictDataDO> labelDictDataBuilder = ImmutableTable.builder();
|
||||||
ImmutableTable.Builder<String, String, SysDictDataDO> valueDictDataBuilder = ImmutableTable.builder();
|
ImmutableTable.Builder<String, String, SysDictDataDO> valueDictDataBuilder = ImmutableTable.builder();
|
||||||
list.forEach(dictData -> {
|
dataList.forEach(dictData -> {
|
||||||
labelDictDataBuilder.put(dictData.getDictType(), dictData.getLabel(), dictData);
|
labelDictDataBuilder.put(dictData.getDictType(), dictData.getLabel(), dictData);
|
||||||
valueDictDataBuilder.put(dictData.getDictType(), dictData.getValue(), dictData);
|
valueDictDataBuilder.put(dictData.getDictType(), dictData.getValue(), dictData);
|
||||||
});
|
});
|
||||||
labelDictDataCache = labelDictDataBuilder.build();
|
labelDictDataCache = labelDictDataBuilder.build();
|
||||||
valueDictDataCache = valueDictDataBuilder.build();
|
valueDictDataCache = valueDictDataBuilder.build();
|
||||||
|
assert dataList.size() > 0; // 断言,避免告警
|
||||||
|
maxUpdateTime = dataList.stream().max(Comparator.comparing(BaseDO::getUpdateTime)).get().getUpdateTime();
|
||||||
|
log.info("[init][缓存字典数据,数量为:{}]", dataList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
|
||||||
|
public void schedulePeriodicRefresh() {
|
||||||
|
initLocalCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 如果字典数据发生变化,从数据库中获取最新的全量字典数据。
|
||||||
|
* 如果未发生变化,则返回空
|
||||||
|
*
|
||||||
|
* @param maxUpdateTime 当前字典数据的最大更新时间
|
||||||
|
* @return 字典数据列表
|
||||||
|
*/
|
||||||
|
private List<SysDictDataDO> loadDictDataIfUpdate(Date maxUpdateTime) {
|
||||||
|
// 第一步,判断是否要更新。
|
||||||
|
if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据
|
||||||
|
log.info("[loadDictDataIfUpdate][首次加载全量字典数据]");
|
||||||
|
} else { // 判断数据库中是否有更新的字典数据
|
||||||
|
if (!dictDataMapper.selectExistsByUpdateTimeAfter(maxUpdateTime)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
log.info("[loadDictDataIfUpdate][增量加载全量字典数据]");
|
||||||
|
}
|
||||||
|
// 第二步,如果有更新,则从数据库加载所有字典数据
|
||||||
|
return dictDataMapper.selectList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -104,6 +160,8 @@ public class SysDictDataServiceImpl implements SysDictDataService {
|
||||||
// 插入字典类型
|
// 插入字典类型
|
||||||
SysDictDataDO dictData = SysDictDataConvert.INSTANCE.convert(reqVO);
|
SysDictDataDO dictData = SysDictDataConvert.INSTANCE.convert(reqVO);
|
||||||
dictDataMapper.insert(dictData);
|
dictDataMapper.insert(dictData);
|
||||||
|
// 发送消息
|
||||||
|
dictDataProducer.sendMenuRefreshMessage();
|
||||||
return dictData.getId();
|
return dictData.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +172,8 @@ public class SysDictDataServiceImpl implements SysDictDataService {
|
||||||
// 更新字典类型
|
// 更新字典类型
|
||||||
SysDictDataDO updateObj = SysDictDataConvert.INSTANCE.convert(reqVO);
|
SysDictDataDO updateObj = SysDictDataConvert.INSTANCE.convert(reqVO);
|
||||||
dictDataMapper.updateById(updateObj);
|
dictDataMapper.updateById(updateObj);
|
||||||
|
// 发送消息
|
||||||
|
dictDataProducer.sendMenuRefreshMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -122,6 +182,8 @@ public class SysDictDataServiceImpl implements SysDictDataService {
|
||||||
this.checkDictDataExists(id);
|
this.checkDictDataExists(id);
|
||||||
// 删除字典数据
|
// 删除字典数据
|
||||||
dictDataMapper.deleteById(id);
|
dictDataMapper.deleteById(id);
|
||||||
|
// 发送消息
|
||||||
|
dictDataProducer.sendMenuRefreshMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -16,9 +16,9 @@ import java.util.List;
|
||||||
public interface SysMenuService {
|
public interface SysMenuService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化菜单
|
* 初始化菜单的本地缓存
|
||||||
*/
|
*/
|
||||||
void init();
|
void initLocalCache();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得所有菜单列表
|
* 获得所有菜单列表
|
||||||
|
|
|
@ -77,8 +77,8 @@ public class SysMenuServiceImpl implements SysMenuService {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public synchronized void init() {
|
public synchronized void initLocalCache() {
|
||||||
// 获取
|
// 获取菜单列表,如果有更新
|
||||||
List<SysMenuDO> menuList = this.loadMenuIfUpdate(maxUpdateTime);
|
List<SysMenuDO> menuList = this.loadMenuIfUpdate(maxUpdateTime);
|
||||||
if (CollUtil.isEmpty(menuList)) {
|
if (CollUtil.isEmpty(menuList)) {
|
||||||
return;
|
return;
|
||||||
|
@ -100,7 +100,7 @@ public class SysMenuServiceImpl implements SysMenuService {
|
||||||
|
|
||||||
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
|
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
|
||||||
public void schedulePeriodicRefresh() {
|
public void schedulePeriodicRefresh() {
|
||||||
init();
|
initLocalCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue