【8.0】【sys】【role】获取角色绑定菜单功能信息,从缓存中读取

pull/57/head
fengshuonan 2023-07-14 23:41:22 +08:00
parent 4d2fcc0c27
commit c35be9e229
5 changed files with 93 additions and 9 deletions

View File

@ -1,4 +1,4 @@
package cn.stylefeng.roses.kernel.sys.modular.role.cache.rolemenu.clear;
package cn.stylefeng.roses.kernel.sys.modular.role.cache;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;

View File

@ -0,0 +1,26 @@
package cn.stylefeng.roses.kernel.sys.modular.role.cache.roleoptions;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
import cn.stylefeng.roses.kernel.sys.modular.role.constants.RoleConstants;
import java.util.List;
/**
*
*
* @author fengshuonan
* @since 2023/7/14 23:23
*/
public class RoleMenuOptionsMemoryCache extends AbstractMemoryCacheOperator<List<Long>> {
public RoleMenuOptionsMemoryCache(TimedCache<String, List<Long>> timedCache) {
super(timedCache);
}
@Override
public String getCommonKeyPrefix() {
return RoleConstants.ROLE_MENU_OPTIONS_CACHE_PREFIX;
}
}

View File

@ -0,0 +1,26 @@
package cn.stylefeng.roses.kernel.sys.modular.role.cache.roleoptions;
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
import cn.stylefeng.roses.kernel.sys.modular.role.constants.RoleConstants;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
/**
*
*
* @author fengshuonan
* @since 2023/7/14 23:23
*/
public class RoleMenuOptionsRedisCache extends AbstractRedisCacheOperator<List<Long>> {
public RoleMenuOptionsRedisCache(RedisTemplate<String, List<Long>> redisTemplate) {
super(redisTemplate);
}
@Override
public String getCommonKeyPrefix() {
return RoleConstants.ROLE_MENU_OPTIONS_CACHE_PREFIX;
}
}

View File

@ -18,4 +18,9 @@ public interface RoleConstants {
*/
String ROLE_BIND_MENU_EVENT = "ROLE_BIND_MENU_EVENT";
/**
*
*/
String ROLE_MENU_OPTIONS_CACHE_PREFIX = "ROLE:MENU_OPTIONS:";
}

View File

@ -1,8 +1,10 @@
package cn.stylefeng.roses.kernel.sys.modular.role.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.sys.api.callback.RemoveMenuCallbackApi;
import cn.stylefeng.roses.kernel.sys.api.callback.RemoveRoleCallbackApi;
import cn.stylefeng.roses.kernel.sys.api.constants.SysConstants;
import cn.stylefeng.roses.kernel.sys.modular.menu.entity.SysMenuOptions;
import cn.stylefeng.roses.kernel.sys.modular.role.action.RoleAssignOperateAction;
import cn.stylefeng.roses.kernel.sys.modular.role.entity.SysRoleMenuOptions;
@ -15,6 +17,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -30,6 +33,9 @@ import java.util.stream.Collectors;
public class SysRoleMenuOptionsServiceImpl extends ServiceImpl<SysRoleMenuOptionsMapper, SysRoleMenuOptions> implements
SysRoleMenuOptionsService, RemoveRoleCallbackApi, RoleAssignOperateAction, RemoveMenuCallbackApi {
@Resource(name = "roleMenuOptionsCache")
private CacheOperatorApi<List<Long>> roleMenuOptionsCache;
@Override
public void removeRoleBindOptions(Long optionsId) {
LambdaQueryWrapper<SysRoleMenuOptions> sysRoleMenuOptionsLambdaQueryWrapper = new LambdaQueryWrapper<>();
@ -65,20 +71,41 @@ public class SysRoleMenuOptionsServiceImpl extends ServiceImpl<SysRoleMenuOption
@Override
public List<Long> getRoleBindMenuOptionsIdList(List<Long> roleIdList) {
List<Long> result = new ArrayList<>();
if (ObjectUtil.isEmpty(roleIdList)) {
return new ArrayList<>();
return result;
}
for (Long roleId : roleIdList) {
String roleIdKey = roleId.toString();
// 先从缓存找到角色绑定的功能
List<Long> optionsCached = roleMenuOptionsCache.get(roleIdKey);
if (ObjectUtil.isNotEmpty(optionsCached)) {
result.addAll(optionsCached);
continue;
}
// 查询数据库角色对应的菜单功能
LambdaQueryWrapper<SysRoleMenuOptions> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRoleMenuOptions::getRoleId, roleIdList);
queryWrapper.eq(SysRoleMenuOptions::getRoleId, roleId);
queryWrapper.select(SysRoleMenuOptions::getMenuOptionId);
List<SysRoleMenuOptions> roleMenuOptions = this.list(queryWrapper);
if (ObjectUtil.isNotEmpty(roleMenuOptions)) {
if (ObjectUtil.isEmpty(roleMenuOptions)) {
return new ArrayList<>();
List<Long> menuOptionsIdQueryResult = roleMenuOptions.stream().map(SysRoleMenuOptions::getMenuOptionId)
.collect(Collectors.toList());
result.addAll(menuOptionsIdQueryResult);
// 添加到缓存中
roleMenuOptionsCache.put(roleIdKey, menuOptionsIdQueryResult, SysConstants.DEFAULT_SYS_CACHE_TIMEOUT_SECONDS);
}
}
return roleMenuOptions.stream().map(SysRoleMenuOptions::getMenuOptionId).collect(Collectors.toList());
return result;
}
@Override