diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemCachesConstants.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemCachesConstants.java index d8c4a0588..eed06ce09 100644 --- a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemCachesConstants.java +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemCachesConstants.java @@ -62,4 +62,9 @@ public interface SystemCachesConstants { */ String ROLE_RESOURCE_CACHE_PREFIX = "role_resource:"; + /** + * 角色绑定的数据范围的缓存 + */ + String ROLE_DATA_SCOPE_CACHE_PREFIX = "role_data_scope:"; + } diff --git a/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/cache/RoleDataScopeMemoryCache.java b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/cache/RoleDataScopeMemoryCache.java new file mode 100644 index 000000000..c118260d5 --- /dev/null +++ b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/cache/RoleDataScopeMemoryCache.java @@ -0,0 +1,50 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.system.modular.role.cache; + +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator; +import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants; + +import java.util.List; + +/** + * 角色绑定的数据范围 + * + * @author fengshuonan + * @date 2021/7/31 17:53 + */ +public class RoleDataScopeMemoryCache extends AbstractMemoryCacheOperator> { + + public RoleDataScopeMemoryCache(TimedCache> timedCache) { + super(timedCache); + } + + @Override + public String getCommonKeyPrefix() { + return SystemCachesConstants.ROLE_DATA_SCOPE_CACHE_PREFIX; + } + +} diff --git a/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/cache/RoleDataScopeRedisCache.java b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/cache/RoleDataScopeRedisCache.java new file mode 100644 index 000000000..15f1a7d11 --- /dev/null +++ b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/cache/RoleDataScopeRedisCache.java @@ -0,0 +1,50 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.system.modular.role.cache; + +import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator; +import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants; +import org.springframework.data.redis.core.RedisTemplate; + +import java.util.List; + +/** + * 角色绑定的数据范围 + * + * @author fengshuonan + * @date 2021/7/31 17:54 + */ +public class RoleDataScopeRedisCache extends AbstractRedisCacheOperator> { + + public RoleDataScopeRedisCache(RedisTemplate> redisTemplate) { + super(redisTemplate); + } + + @Override + public String getCommonKeyPrefix() { + return SystemCachesConstants.ROLE_INFO_CACHE_PREFIX; + } + +} \ No newline at end of file diff --git a/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/SysRoleDataScopeService.java b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/SysRoleDataScopeService.java index 382456a72..7a7a18b3b 100644 --- a/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/SysRoleDataScopeService.java +++ b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/SysRoleDataScopeService.java @@ -24,9 +24,9 @@ */ package cn.stylefeng.roses.kernel.system.modular.role.service; -import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRoleDataScope; import cn.stylefeng.roses.kernel.system.api.pojo.role.request.SysRoleDataScopeRequest; import cn.stylefeng.roses.kernel.system.api.pojo.role.request.SysRoleRequest; +import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRoleDataScope; import com.baomidou.mybatisplus.extension.service.IService; import java.util.List; @@ -58,7 +58,6 @@ public interface SysRoleDataScopeService extends IService { */ List getRoleDataScopeIdList(List roleIdList); - /** * 新增 * diff --git a/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/impl/SysRoleServiceImpl.java b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/impl/SysRoleServiceImpl.java index 464eaa983..c010844e1 100644 --- a/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/impl/SysRoleServiceImpl.java +++ b/kernel-s-system/system-business-role/src/main/java/cn/stylefeng/roses/kernel/system/modular/role/service/impl/SysRoleServiceImpl.java @@ -103,6 +103,9 @@ public class SysRoleServiceImpl extends ServiceImpl impl @Resource(name = "roleResourceCacheApi") private CacheOperatorApi> roleResourceCacheApi; + @Resource(name = "roleDataScopeCacheApi") + private CacheOperatorApi> roleDataScopeCacheApi; + @Override public void add(SysRoleRequest sysRoleRequest) { @@ -149,6 +152,9 @@ public class SysRoleServiceImpl extends ServiceImpl impl // 删除角色缓存信息 roleInfoCacheApi.remove(String.valueOf(roleId)); + + // 删除角色的数据范围缓存 + roleDataScopeCacheApi.remove(String.valueOf(sysRoleRequest.getRoleId())); } @Override @@ -314,6 +320,9 @@ public class SysRoleServiceImpl extends ServiceImpl impl // 绑定角色数据范围关联 sysRoleDataScopeService.grantDataScope(sysRoleRequest); + + // 删除角色的数据范围缓存 + roleDataScopeCacheApi.remove(String.valueOf(sysRoleRequest.getRoleId())); } @Override @@ -368,15 +377,35 @@ public class SysRoleServiceImpl extends ServiceImpl impl @Override public List getRoleDataScopes(List roleIds) { - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.in(SysRoleDataScope::getRoleId, roleIds); - List list = this.sysRoleDataScopeService.list(queryWrapper); - if (!list.isEmpty()) { - return list.stream().map(SysRoleDataScope::getOrganizationId).collect(Collectors.toList()); - } else { - return new ArrayList<>(); + ArrayList result = new ArrayList<>(); + + if (ObjectUtil.isEmpty(roleIds)) { + return result; } + + for (Long roleId : roleIds) { + // 从缓存获取数据范围 + String key = String.valueOf(roleId); + List scopes = roleDataScopeCacheApi.get(key); + if (scopes != null) { + result.addAll(scopes); + } + + // 从数据库查询数据范围 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(SysRoleDataScope::getRoleId, roleId); + List list = this.sysRoleDataScopeService.list(queryWrapper); + if (!list.isEmpty()) { + List realScopes = list.stream().map(SysRoleDataScope::getOrganizationId).collect(Collectors.toList()); + result.addAll(realScopes); + + // 添加结果到缓存中 + roleDataScopeCacheApi.put(key, realScopes); + } + } + + return result; } @Override diff --git a/kernel-s-system/system-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/system/starter/GunsSystemCacheAutoConfiguration.java b/kernel-s-system/system-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/system/starter/GunsSystemCacheAutoConfiguration.java index 2f3cb97c4..6b1be277a 100644 --- a/kernel-s-system/system-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/system/starter/GunsSystemCacheAutoConfiguration.java +++ b/kernel-s-system/system-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/system/starter/GunsSystemCacheAutoConfiguration.java @@ -30,6 +30,7 @@ import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants; import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO; import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserOrgDTO; +import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleDataScopeMemoryCache; import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleMemoryCache; import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleResourceMemoryCache; import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRole; @@ -116,4 +117,17 @@ public class GunsSystemCacheAutoConfiguration { return new RoleResourceMemoryCache(roleCache); } + /** + * 角色绑定的数据范围的缓存 + * + * @author fengshuonan + * @date 2021/7/31 17:59 + */ + @Bean + @ConditionalOnMissingBean(name = "roleDataScopeCacheApi") + public CacheOperatorApi> roleDataScopeCacheApi() { + TimedCache> roleCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000); + return new RoleDataScopeMemoryCache(roleCache); + } + }