【7.1.1】更新角色绑定数据范围缓存

pull/22/head
fengshuonan 2021-07-31 18:21:04 +08:00
parent d275012f11
commit 5895d7d24e
6 changed files with 156 additions and 9 deletions

View File

@ -62,4 +62,9 @@ public interface SystemCachesConstants {
*/
String ROLE_RESOURCE_CACHE_PREFIX = "role_resource:";
/**
*
*/
String ROLE_DATA_SCOPE_CACHE_PREFIX = "role_data_scope:";
}

View File

@ -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.
*
* GunsAPACHE 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;
}
}

View File

@ -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.
*
* GunsAPACHE 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;
}
}

View File

@ -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);
/**
*
*

View File

@ -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

View File

@ -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);
}
}