From c35be9e22949231fc4a23dfe09a6dc87cf92dc5e Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Fri, 14 Jul 2023 23:41:22 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.0=E3=80=91=E3=80=90sys=E3=80=91?= =?UTF-8?q?=E3=80=90role=E3=80=91=E8=8E=B7=E5=8F=96=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E8=8F=9C=E5=8D=95=E5=8A=9F=E8=83=BD=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E4=BB=8E=E7=BC=93=E5=AD=98=E4=B8=AD=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../clear => }/RoleMenuClearListener.java | 2 +- .../RoleMenuOptionsMemoryCache.java | 26 +++++++++++ .../RoleMenuOptionsRedisCache.java | 26 +++++++++++ .../modular/role/constants/RoleConstants.java | 5 +++ .../impl/SysRoleMenuOptionsServiceImpl.java | 43 +++++++++++++++---- 5 files changed, 93 insertions(+), 9 deletions(-) rename kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/{rolemenu/clear => }/RoleMenuClearListener.java (92%) create mode 100644 kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsMemoryCache.java create mode 100644 kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsRedisCache.java diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/rolemenu/clear/RoleMenuClearListener.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/RoleMenuClearListener.java similarity index 92% rename from kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/rolemenu/clear/RoleMenuClearListener.java rename to kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/RoleMenuClearListener.java index db77b47e4..ee28e977e 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/rolemenu/clear/RoleMenuClearListener.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/RoleMenuClearListener.java @@ -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; diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsMemoryCache.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsMemoryCache.java new file mode 100644 index 000000000..b0336f569 --- /dev/null +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsMemoryCache.java @@ -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> { + + public RoleMenuOptionsMemoryCache(TimedCache> timedCache) { + super(timedCache); + } + + @Override + public String getCommonKeyPrefix() { + return RoleConstants.ROLE_MENU_OPTIONS_CACHE_PREFIX; + } + +} \ No newline at end of file diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsRedisCache.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsRedisCache.java new file mode 100644 index 000000000..56239a21c --- /dev/null +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/cache/roleoptions/RoleMenuOptionsRedisCache.java @@ -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> { + + public RoleMenuOptionsRedisCache(RedisTemplate> redisTemplate) { + super(redisTemplate); + } + + @Override + public String getCommonKeyPrefix() { + return RoleConstants.ROLE_MENU_OPTIONS_CACHE_PREFIX; + } + +} diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/constants/RoleConstants.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/constants/RoleConstants.java index 12e2d04d2..44958c34e 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/constants/RoleConstants.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/constants/RoleConstants.java @@ -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:"; + } diff --git a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleMenuOptionsServiceImpl.java b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleMenuOptionsServiceImpl.java index 8aeb680e4..cc78ea16c 100644 --- a/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleMenuOptionsServiceImpl.java +++ b/kernel-s-system/system-business-permission/src/main/java/cn/stylefeng/roses/kernel/sys/modular/role/service/impl/SysRoleMenuOptionsServiceImpl.java @@ -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 implements SysRoleMenuOptionsService, RemoveRoleCallbackApi, RoleAssignOperateAction, RemoveMenuCallbackApi { + @Resource(name = "roleMenuOptionsCache") + private CacheOperatorApi> roleMenuOptionsCache; + @Override public void removeRoleBindOptions(Long optionsId) { LambdaQueryWrapper sysRoleMenuOptionsLambdaQueryWrapper = new LambdaQueryWrapper<>(); @@ -65,20 +71,41 @@ public class SysRoleMenuOptionsServiceImpl extends ServiceImpl getRoleBindMenuOptionsIdList(List roleIdList) { + List result = new ArrayList<>(); + if (ObjectUtil.isEmpty(roleIdList)) { - return new ArrayList<>(); + return result; } - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.in(SysRoleMenuOptions::getRoleId, roleIdList); - queryWrapper.select(SysRoleMenuOptions::getMenuOptionId); - List roleMenuOptions = this.list(queryWrapper); + for (Long roleId : roleIdList) { - if (ObjectUtil.isEmpty(roleMenuOptions)) { - return new ArrayList<>(); + String roleIdKey = roleId.toString(); + + // 先从缓存找到角色绑定的功能 + List optionsCached = roleMenuOptionsCache.get(roleIdKey); + + if (ObjectUtil.isNotEmpty(optionsCached)) { + result.addAll(optionsCached); + continue; + } + + // 查询数据库角色对应的菜单功能 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(SysRoleMenuOptions::getRoleId, roleId); + queryWrapper.select(SysRoleMenuOptions::getMenuOptionId); + List roleMenuOptions = this.list(queryWrapper); + if (ObjectUtil.isNotEmpty(roleMenuOptions)) { + + List 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