mirror of https://gitee.com/stylefeng/roses
【7.1.1】更新角色绑定数据范围缓存
parent
d275012f11
commit
5895d7d24e
|
@ -62,4 +62,9 @@ public interface SystemCachesConstants {
|
|||
*/
|
||||
String ROLE_RESOURCE_CACHE_PREFIX = "role_resource:";
|
||||
|
||||
/**
|
||||
* 角色绑定的数据范围的缓存
|
||||
*/
|
||||
String ROLE_DATA_SCOPE_CACHE_PREFIX = "role_data_scope:";
|
||||
|
||||
}
|
||||
|
|
|
@ -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<List<Long>> {
|
||||
|
||||
public RoleDataScopeMemoryCache(TimedCache<String, List<Long>> timedCache) {
|
||||
super(timedCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return SystemCachesConstants.ROLE_DATA_SCOPE_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<List<Long>> {
|
||||
|
||||
public RoleDataScopeRedisCache(RedisTemplate<String, List<Long>> redisTemplate) {
|
||||
super(redisTemplate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return SystemCachesConstants.ROLE_INFO_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<SysRoleDataScope> {
|
|||
*/
|
||||
List<Long> getRoleDataScopeIdList(List<Long> roleIdList);
|
||||
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
|
|
|
@ -103,6 +103,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
@Resource(name = "roleResourceCacheApi")
|
||||
private CacheOperatorApi<List<String>> roleResourceCacheApi;
|
||||
|
||||
@Resource(name = "roleDataScopeCacheApi")
|
||||
private CacheOperatorApi<List<Long>> roleDataScopeCacheApi;
|
||||
|
||||
@Override
|
||||
public void add(SysRoleRequest sysRoleRequest) {
|
||||
|
||||
|
@ -149,6 +152,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
// 删除角色缓存信息
|
||||
roleInfoCacheApi.remove(String.valueOf(roleId));
|
||||
|
||||
// 删除角色的数据范围缓存
|
||||
roleDataScopeCacheApi.remove(String.valueOf(sysRoleRequest.getRoleId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -314,6 +320,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
// 绑定角色数据范围关联
|
||||
sysRoleDataScopeService.grantDataScope(sysRoleRequest);
|
||||
|
||||
// 删除角色的数据范围缓存
|
||||
roleDataScopeCacheApi.remove(String.valueOf(sysRoleRequest.getRoleId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -368,15 +377,35 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||
|
||||
@Override
|
||||
public List<Long> getRoleDataScopes(List<Long> roleIds) {
|
||||
LambdaQueryWrapper<SysRoleDataScope> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.in(SysRoleDataScope::getRoleId, roleIds);
|
||||
|
||||
List<SysRoleDataScope> list = this.sysRoleDataScopeService.list(queryWrapper);
|
||||
if (!list.isEmpty()) {
|
||||
return list.stream().map(SysRoleDataScope::getOrganizationId).collect(Collectors.toList());
|
||||
} else {
|
||||
return new ArrayList<>();
|
||||
ArrayList<Long> result = new ArrayList<>();
|
||||
|
||||
if (ObjectUtil.isEmpty(roleIds)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (Long roleId : roleIds) {
|
||||
// 从缓存获取数据范围
|
||||
String key = String.valueOf(roleId);
|
||||
List<Long> scopes = roleDataScopeCacheApi.get(key);
|
||||
if (scopes != null) {
|
||||
result.addAll(scopes);
|
||||
}
|
||||
|
||||
// 从数据库查询数据范围
|
||||
LambdaQueryWrapper<SysRoleDataScope> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(SysRoleDataScope::getRoleId, roleId);
|
||||
List<SysRoleDataScope> list = this.sysRoleDataScopeService.list(queryWrapper);
|
||||
if (!list.isEmpty()) {
|
||||
List<Long> realScopes = list.stream().map(SysRoleDataScope::getOrganizationId).collect(Collectors.toList());
|
||||
result.addAll(realScopes);
|
||||
|
||||
// 添加结果到缓存中
|
||||
roleDataScopeCacheApi.put(key, realScopes);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -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<List<Long>> roleDataScopeCacheApi() {
|
||||
TimedCache<String, List<Long>> roleCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
|
||||
return new RoleDataScopeMemoryCache(roleCache);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue