【7.1.1】【cache】优化用户和资源绑定的缓存

pull/22/head
fengshuonan 2021-07-30 23:42:21 +08:00
parent 9a463b4970
commit d275012f11
6 changed files with 175 additions and 6 deletions

View File

@ -57,4 +57,9 @@ public interface SystemCachesConstants {
*/
String USER_ORG_CACHE_PREFIX = "user_org:";
/**
*
*/
String ROLE_RESOURCE_CACHE_PREFIX = "role_resource:";
}

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/30 23:20
*/
public class RoleResourceMemoryCache extends AbstractMemoryCacheOperator<List<String>> {
public RoleResourceMemoryCache(TimedCache<String, List<String>> timedCache) {
super(timedCache);
}
@Override
public String getCommonKeyPrefix() {
return SystemCachesConstants.ROLE_RESOURCE_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/29 23:29
*/
public class RoleResourceRedisCache extends AbstractRedisCacheOperator<List<String>> {
public RoleResourceRedisCache(RedisTemplate<String, List<String>> redisTemplate) {
super(redisTemplate);
}
@Override
public String getCommonKeyPrefix() {
return SystemCachesConstants.ROLE_RESOURCE_CACHE_PREFIX;
}
}

View File

@ -24,17 +24,20 @@
*/
package cn.stylefeng.roses.kernel.system.modular.role.service.impl;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.system.api.pojo.role.request.SysRoleRequest;
import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRoleResource;
import cn.stylefeng.roses.kernel.system.modular.role.mapper.SysRoleResourceMapper;
import cn.stylefeng.roses.kernel.system.modular.role.service.SysRoleResourceService;
import cn.stylefeng.roses.kernel.system.api.pojo.role.request.SysRoleRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* service
@ -45,6 +48,8 @@ import java.util.List;
@Service
public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMapper, SysRoleResource> implements SysRoleResourceService {
@Resource(name = "roleResourceCacheApi")
private CacheOperatorApi<List<String>> roleResourceCacheApi;
@Override
@Transactional(rollbackFor = Exception.class)
@ -57,6 +62,9 @@ public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMappe
queryWrapper.eq(SysRoleResource::getRoleId, roleId);
this.remove(queryWrapper);
// 清除角色缓存资源
roleResourceCacheApi.remove(String.valueOf(roleId));
// 授权资源
List<String> grantResourceList = sysRoleRequest.getGrantResourceList();
ArrayList<SysRoleResource> sysRoleResources = new ArrayList<>();
@ -74,6 +82,20 @@ public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMappe
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteRoleResourceListByResourceIds(List<Long> resourceIds) {
// 查询资源包含的角色
LambdaQueryWrapper<SysRoleResource> wrapper1 = new LambdaQueryWrapper<>();
wrapper1.select(SysRoleResource::getRoleId);
wrapper1.in(SysRoleResource::getResourceCode, resourceIds);
wrapper1.groupBy(SysRoleResource::getRoleId);
List<SysRoleResource> toGetRoles = this.list(wrapper1);
List<Long> roleIds = toGetRoles.stream().map(SysRoleResource::getRoleId).collect(Collectors.toList());
for (Long roleId : roleIds) {
// 清除角色绑定的资源缓存
roleResourceCacheApi.remove(String.valueOf(roleId));
}
// 清除资源
LambdaQueryWrapper<SysRoleResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRoleResource::getResourceCode, resourceIds);
this.remove(queryWrapper);
@ -85,6 +107,10 @@ public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMappe
LambdaQueryWrapper<SysRoleResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysRoleResource::getRoleId, roleId);
this.remove(queryWrapper);
// 清除角色绑定的资源缓存
roleResourceCacheApi.remove(String.valueOf(roleId));
}
}

View File

@ -100,6 +100,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Resource
private CacheOperatorApi<SysRole> roleInfoCacheApi;
@Resource(name = "roleResourceCacheApi")
private CacheOperatorApi<List<String>> roleResourceCacheApi;
@Override
public void add(SysRoleRequest sysRoleRequest) {
@ -398,11 +401,32 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Override
public List<String> getRoleResourceCodeList(List<Long> roleIdList) {
LambdaQueryWrapper<SysRoleResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(SysRoleResource::getResourceCode);
queryWrapper.in(SysRoleResource::getRoleId, roleIdList);
List<SysRoleResource> sysRoleResources = sysRoleResourceService.list(queryWrapper);
return sysRoleResources.parallelStream().map(SysRoleResource::getResourceCode).collect(Collectors.toList());
ArrayList<String> result = new ArrayList<>();
for (Long roleId : roleIdList) {
// 从缓存获取所有角色绑定的资源
String key = String.valueOf(roleId);
List<String> resourceCodesCache = roleResourceCacheApi.get(key);
if (ObjectUtil.isNotEmpty(resourceCodesCache)) {
result.addAll(resourceCodesCache);
continue;
}
// 从数据库查询角色绑定的资源
LambdaQueryWrapper<SysRoleResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(SysRoleResource::getResourceCode);
queryWrapper.eq(SysRoleResource::getRoleId, roleId);
List<SysRoleResource> sysRoleResources = sysRoleResourceService.list(queryWrapper);
List<String> sysResourceCodes = sysRoleResources.parallelStream().map(SysRoleResource::getResourceCode).collect(Collectors.toList());
if (ObjectUtil.isNotEmpty(sysResourceCodes)) {
result.addAll(sysResourceCodes);
roleResourceCacheApi.put(key, sysResourceCodes);
}
}
return result;
}
@Override

View File

@ -31,6 +31,7 @@ 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.RoleMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleResourceMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRole;
import cn.stylefeng.roses.kernel.system.modular.user.cache.SysUserMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.user.cache.UserOrgMemoryCache;
@ -102,4 +103,17 @@ public class GunsSystemCacheAutoConfiguration {
return new UserOrgMemoryCache(roleCache);
}
/**
*
*
* @author fengshuonan
* @date 2021/7/30 23:29
*/
@Bean
@ConditionalOnMissingBean(name = "roleResourceCacheApi")
public CacheOperatorApi<List<String>> roleResourceCacheApi() {
TimedCache<String, List<String>> roleCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
return new RoleResourceMemoryCache(roleCache);
}
}