【7.1.1】【role】增加角色信息的缓存

pull/22/head
fengshuonan 2021-07-29 23:44:53 +08:00
parent 6b1c426a0f
commit 4345b07493
6 changed files with 79 additions and 32 deletions

View File

@ -54,6 +54,26 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--缓存api-->
<!--角色的查询会使用缓存-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>cache-api</artifactId>
<version>${roses.version}</version>
</dependency>
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>cache-sdk-memory</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>cache-sdk-redis</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -22,12 +22,12 @@
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.system.modular.user.cache;
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 cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleDTO;
import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRole;
/**
*
@ -35,9 +35,9 @@ import cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleDTO;
* @author fengshuonan
* @date 2021/7/29 23:28
*/
public class RoleMemoryCache extends AbstractMemoryCacheOperator<SysRoleDTO> {
public class RoleMemoryCache extends AbstractMemoryCacheOperator<SysRole> {
public RoleMemoryCache(TimedCache<String, SysRoleDTO> timedCache) {
public RoleMemoryCache(TimedCache<String, SysRole> timedCache) {
super(timedCache);
}

View File

@ -22,11 +22,11 @@
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.system.modular.user.cache;
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 cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleDTO;
import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRole;
import org.springframework.data.redis.core.RedisTemplate;
/**
@ -35,9 +35,9 @@ import org.springframework.data.redis.core.RedisTemplate;
* @author fengshuonan
* @date 2021/7/29 23:29
*/
public class RoleRedisCache extends AbstractRedisCacheOperator<SysRoleDTO> {
public class RoleRedisCache extends AbstractRedisCacheOperator<SysRole> {
public RoleRedisCache(RedisTemplate<String, SysRoleDTO> redisTemplate) {
public RoleRedisCache(RedisTemplate<String, SysRole> redisTemplate) {
super(redisTemplate);
}

View File

@ -32,6 +32,7 @@ import cn.stylefeng.roses.kernel.auth.api.enums.DataScopeTypeEnum;
import cn.stylefeng.roses.kernel.auth.api.exception.AuthException;
import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
@ -45,9 +46,6 @@ import cn.stylefeng.roses.kernel.system.api.UserServiceApi;
import cn.stylefeng.roses.kernel.system.api.constants.SystemConstants;
import cn.stylefeng.roses.kernel.system.api.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.api.exception.enums.role.SysRoleExceptionEnum;
import cn.stylefeng.roses.kernel.system.modular.role.entity.*;
import cn.stylefeng.roses.kernel.system.modular.role.mapper.SysRoleMapper;
import cn.stylefeng.roses.kernel.system.modular.role.service.*;
import cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleMenuButtonDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleMenuDTO;
@ -55,6 +53,9 @@ import cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleResourceDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.role.request.SysRoleMenuButtonRequest;
import cn.stylefeng.roses.kernel.system.api.pojo.role.request.SysRoleRequest;
import cn.stylefeng.roses.kernel.system.api.util.DataScopeUtil;
import cn.stylefeng.roses.kernel.system.modular.role.entity.*;
import cn.stylefeng.roses.kernel.system.modular.role.mapper.SysRoleMapper;
import cn.stylefeng.roses.kernel.system.modular.role.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -96,6 +97,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Resource
private MenuServiceApi menuServiceApi;
@Resource
private CacheOperatorApi<SysRole> roleInfoCacheApi;
@Override
public void add(SysRoleRequest sysRoleRequest) {
@ -139,9 +143,13 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
// 级联删除该角色对应的角色-菜单表关联信息
sysRoleResourceService.deleteRoleResourceListByRoleId(roleId);
// 删除角色缓存信息
roleInfoCacheApi.remove(String.valueOf(roleId));
}
@Override
@Transactional(rollbackFor = Exception.class)
public void edit(SysRoleRequest sysRoleRequest) {
SysRole sysRole = this.querySysRole(sysRoleRequest);
@ -159,6 +167,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
sysRole.setStatusFlag(null);
this.updateById(sysRole);
// 删除角色缓存信息
roleInfoCacheApi.remove(String.valueOf(sysRoleRequest.getRoleId()));
}
@Override
@ -342,24 +353,13 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Override
public List<SysRoleDTO> getRolesByIds(List<Long> roleIds) {
ArrayList<SysRoleDTO> sysRoleResponses = new ArrayList<>();
LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRole::getRoleId, roleIds);
List<SysRole> sysRoles = this.list(queryWrapper);
// 角色列表不为空角色信息转化为SysRoleResponse
if (!sysRoles.isEmpty()) {
for (SysRole sysRole : sysRoles) {
SysRoleDTO sysRoleResponse = new SysRoleDTO();
BeanUtil.copyProperties(sysRole, sysRoleResponse);
// 填充数据范围类型枚举
sysRoleResponse.setDataScopeTypeEnum(DataScopeTypeEnum.codeToEnum(sysRole.getDataScopeType()));
sysRoleResponses.add(sysRoleResponse);
}
for (Long roleId : roleIds) {
SysRoleRequest sysRoleRequest = new SysRoleRequest();
sysRoleRequest.setRoleId(roleId);
SysRoleDTO detail = this.detail(sysRoleRequest);
sysRoleResponses.add(detail);
}
return sysRoleResponses;
}
@ -446,10 +446,22 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
* @date 2020/11/5 4:12
*/
private SysRole querySysRole(SysRoleRequest sysRoleRequest) {
// 从缓存中获取角色信息
String key = String.valueOf(sysRoleRequest.getRoleId());
SysRole sysRoleCache = roleInfoCacheApi.get(key);
if (sysRoleCache != null) {
return sysRoleCache;
}
SysRole sysRole = this.getById(sysRoleRequest.getRoleId());
if (ObjectUtil.isNull(sysRole)) {
throw new SystemModularException(SysRoleExceptionEnum.ROLE_NOT_EXIST);
}
// 加入缓存
roleInfoCacheApi.put(key, sysRole);
return sysRole;
}

View File

@ -29,6 +29,8 @@ import cn.hutool.cache.impl.TimedCache;
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.modular.role.cache.RoleMemoryCache;
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.UserRoleMemoryCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -38,13 +40,13 @@ import org.springframework.context.annotation.Configuration;
import java.util.List;
/**
*
*
*
* @author fengshuonan
* @date 2021/2/28 10:29
*/
@Configuration
public class GunsUserCacheAutoConfiguration {
public class GunsSystemCacheAutoConfiguration {
/**
* 线
@ -68,8 +70,21 @@ public class GunsUserCacheAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "userRoleCacheApi")
public CacheOperatorApi<List<Long>> userRoleCacheApi() {
TimedCache<String, List<Long>> sysUserTimedCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
return new UserRoleMemoryCache(sysUserTimedCache);
TimedCache<String, List<Long>> userRoleCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
return new UserRoleMemoryCache(userRoleCache);
}
/**
*
*
* @author fengshuonan
* @date 2021/7/29 23:00
*/
@Bean
@ConditionalOnMissingBean(name = "roleInfoCacheApi")
public CacheOperatorApi<SysRole> roleInfoCacheApi() {
TimedCache<String, SysRole> roleCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
return new RoleMemoryCache(roleCache);
}
}

View File

@ -1,3 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.system.starter.GunsUserCacheAutoConfiguration,\
cn.stylefeng.roses.kernel.system.starter.GunsSystemCacheAutoConfiguration,\
cn.stylefeng.roses.kernel.system.starter.GunsResourceCacheAutoConfiguration