mirror of https://gitee.com/stylefeng/roses
【7.1.1】【cache】优化用户和资源绑定的缓存
parent
9a463b4970
commit
d275012f11
|
@ -57,4 +57,9 @@ public interface SystemCachesConstants {
|
|||
*/
|
||||
String USER_ORG_CACHE_PREFIX = "user_org:";
|
||||
|
||||
/**
|
||||
* 角色绑定资源的缓存
|
||||
*/
|
||||
String ROLE_RESOURCE_CACHE_PREFIX = "role_resource:";
|
||||
|
||||
}
|
||||
|
|
|
@ -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/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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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/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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue