优化角色菜单、角色用户、用户角色授权没有立即生效:

1、逐个删除key -> 批量删除;
2、操作人操作其他机构时才显示删除;
3、日志输出优化;
This commit is contained in:
gaoqr
2024-06-21 10:51:13 +08:00
parent 1bca13a0f7
commit 666cdb08c2
@@ -3,6 +3,7 @@ package com.cf.imes.module.system.service.permission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.cf.imes.framework.common.enums.CommonStatusEnum;
import com.cf.imes.framework.common.exception.ServiceException;
@@ -189,18 +190,22 @@ public class PermissionServiceImpl implements PermissionService {
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
}
Long currentOrganId = SecurityFrameworkUtils.getUserOrganId();
// 角色菜单权限发生修改,通知刷新本地缓存
if (CollectionUtil.isNotEmpty(createMenuIds) || CollectionUtil.isNotEmpty(deleteMenuIds)) {
CompletableFuture.runAsync(() -> {
// 获取新增和删除菜单id的并集
Collection<Long> delKeys = CollUtil.union(createMenuIds, deleteMenuIds);
delKeys.forEach(menuId -> {
// 移除redis中的缓存
redisTemplate.delete(String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), organId, menuId));
});
// 当前操作人和所操作的目标菜单机构不同时,清空MENU_ROLE_ID_LIST:目标机构id:菜单id的缓存
if (ObjectUtil.notEqual(organId, currentOrganId)) {
// 获取新增和删除菜单id的并集
Collection<Long> delMenuIds = CollUtil.union(createMenuIds, deleteMenuIds);
// 构建批量删除的key集合
Set<String> delKeys = delMenuIds.stream().map(menuId -> String.format(String.valueOf(new StringBuffer(RedisKeyConstants.MENU_ROLE_ID_LIST).append(":%s:%s")), organId, menuId)).collect(Collectors.toSet());
Long batchDelNum = redisTemplate.delete(delKeys);
log.info("[assignRoleMenu] 批量删除redis[MENU_ROLE_ID_LIST]数量:{}", batchDelNum);
}
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
}).exceptionally(e -> {
log.error("redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e.getMessage());
log.error("[assignRoleMenu] redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e);
return null;
});
}
@@ -326,18 +331,22 @@ public class PermissionServiceImpl implements PermissionService {
userRoleMapper.deleteListByRoleIdAndUserIds(roleId, deleteRoleUserIds);
}
Long currentOrganId = SecurityFrameworkUtils.getUserOrganId();
// 角色菜单权限发生修改,通知刷新本地缓存
if (CollectionUtil.isNotEmpty(createRoleUserIds) || CollectionUtil.isNotEmpty(deleteRoleUserIds)) {
CompletableFuture.runAsync(() -> {
// 获取新增和删除用户id的并集
Collection<Long> delKeys = CollUtil.union(createRoleUserIds, deleteRoleUserIds);
delKeys.forEach(userId -> {
// 移除redis中的缓存
redisTemplate.delete(String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), finalOrganId, userId));
});
// 当前操作人和所操作的目标菜单机构不同时,清空USER_ROLE_ID_LIST:目标机构id:用户id的缓存
if (ObjectUtil.notEqual(finalOrganId, currentOrganId)) {
// 获取新增和删除用户id的并集
Collection<Long> delRoleUserIds = CollUtil.union(createRoleUserIds, deleteRoleUserIds);
// 构建批量删除的key集合
Set<String> delKeys = delRoleUserIds.stream().map(userId -> String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), finalOrganId, userId)).collect(Collectors.toSet());
Long batchDelNum = redisTemplate.delete(delKeys);
log.info("[batchAssignRoleUsers] 批量删除redis[USER_ROLE_ID_LIST]数量:{}", batchDelNum);
}
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
}).exceptionally(e -> {
log.error("redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e.getMessage());
log.error("[batchAssignRoleUsers] redis message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e);
return null;
});
}
@@ -419,7 +428,7 @@ public class PermissionServiceImpl implements PermissionService {
redisTemplate.delete(String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), organId, userId));
redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, "");
}).exceptionally(e -> {
log.error("redis 清空user_role_ids或message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e.getMessage());
log.error("[assignUserRole] redis 清空USER_ROLE_ID_LIST或message发送失败, topic:{}, 异常:{}", RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, e);
return null;
});
}