diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemCachesConstants.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemCachesConstants.java new file mode 100644 index 000000000..888232f6c --- /dev/null +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemCachesConstants.java @@ -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.api.constants; + +/** + * 缓存前缀相关的常量 + * + * @author fengshuonan + * @date 2021/7/29 22:55 + */ +public interface SystemCachesConstants { + + /** + * 用户缓存的前缀 + */ + String USER_CACHE_PREFIX = "user:"; + + /** + * 用户缓存过期时间(1小时) + */ + Long USER_CACHE_TIMEOUT_SECONDS = 3600L; + + /** + * 用户绑定的角色的缓存前缀 + */ + String USER_ROLES_CACHE_PREFIX = "user_roles:"; + +} diff --git a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemConstants.java b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemConstants.java index b693ca93b..bc1a4b2d4 100644 --- a/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemConstants.java +++ b/kernel-s-system/system-api/src/main/java/cn/stylefeng/roses/kernel/system/api/constants/SystemConstants.java @@ -57,16 +57,6 @@ public interface SystemConstants { */ String DEFAULT_SYSTEM_NAME = "Guns快速开发平台"; - /** - * 用户缓存的前缀 - */ - String USER_CACHE_PREFIX = "user:"; - - /** - * 用户缓存过期时间(1小时) - */ - Long USER_CACHE_TIMEOUT_SECONDS = 3600L; - /** * 超级管理员的角色编码 */ diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserMemoryCache.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserMemoryCache.java index 92b3e265b..f0068dd18 100644 --- a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserMemoryCache.java +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserMemoryCache.java @@ -26,7 +26,7 @@ package cn.stylefeng.roses.kernel.system.modular.user.cache; import cn.hutool.cache.impl.TimedCache; import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator; -import cn.stylefeng.roses.kernel.system.api.constants.SystemConstants; +import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants; import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO; /** @@ -43,7 +43,7 @@ public class SysUserMemoryCache extends AbstractMemoryCacheOperator @Override public String getCommonKeyPrefix() { - return SystemConstants.USER_CACHE_PREFIX; + return SystemCachesConstants.USER_CACHE_PREFIX; } } diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserRedisCache.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserRedisCache.java index 8d1c390b4..e3ba60839 100644 --- a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserRedisCache.java +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/SysUserRedisCache.java @@ -25,7 +25,7 @@ package cn.stylefeng.roses.kernel.system.modular.user.cache; import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator; -import cn.stylefeng.roses.kernel.system.api.constants.SystemConstants; +import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants; import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO; import org.springframework.data.redis.core.RedisTemplate; @@ -43,7 +43,7 @@ public class SysUserRedisCache extends AbstractRedisCacheOperator { @Override public String getCommonKeyPrefix() { - return SystemConstants.USER_CACHE_PREFIX; + return SystemCachesConstants.USER_CACHE_PREFIX; } } diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/UserRoleMemoryCache.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/UserRoleMemoryCache.java new file mode 100644 index 000000000..1c770e562 --- /dev/null +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/cache/UserRoleMemoryCache.java @@ -0,0 +1,52 @@ +/* + * 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.user.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; + +/** + * 用户角色的内存缓存 + *

+ * key为userId,value是Long类型集合,为角色的集合 + * + * @author fengshuonan + * @date 2021/7/29 22:54 + */ +public class UserRoleMemoryCache extends AbstractMemoryCacheOperator> { + + public UserRoleMemoryCache(TimedCache> timedCache) { + super(timedCache); + } + + @Override + public String getCommonKeyPrefix() { + return SystemCachesConstants.USER_ROLES_CACHE_PREFIX; + } + +} diff --git a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java index 0ffa9687a..81e503ec5 100644 --- a/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java +++ b/kernel-s-system/system-business-user/src/main/java/cn/stylefeng/roses/kernel/system/modular/user/service/impl/SysUserRoleServiceImpl.java @@ -27,16 +27,18 @@ package cn.stylefeng.roses.kernel.system.modular.user.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; +import cn.stylefeng.roses.kernel.system.api.pojo.user.request.SysUserRequest; +import cn.stylefeng.roses.kernel.system.api.pojo.user.request.UserRoleRequest; import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUserRole; import cn.stylefeng.roses.kernel.system.modular.user.mapper.SysUserRoleMapper; import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserRoleService; -import cn.stylefeng.roses.kernel.system.api.pojo.user.request.SysUserRequest; -import cn.stylefeng.roses.kernel.system.api.pojo.user.request.UserRoleRequest; 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.List; import java.util.stream.Collectors; @@ -49,6 +51,8 @@ import java.util.stream.Collectors; @Service public class SysUserRoleServiceImpl extends ServiceImpl implements SysUserRoleService { + @Resource(name = "userRoleCacheApi") + private CacheOperatorApi> userRoleCacheApi; @Override public void add(UserRoleRequest userRoleRequest) { @@ -99,13 +103,23 @@ public class SysUserRoleServiceImpl extends ServiceImpl findRoleIdsByUserId(Long userId) { + + // 先从缓存获取用户绑定的角色 + String key = String.valueOf(userId); + List userRolesCache = userRoleCacheApi.get(key); + if (userRolesCache != null) { + return userRolesCache; + } + + // 从数据库查询角色 UserRoleRequest userRoleRequest = new UserRoleRequest(); userRoleRequest.setUserId(userId); List sysUserRoleList = this.findList(userRoleRequest); - return sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList()); + List userRoles = sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList()); + userRoleCacheApi.put(key, userRoles); + return userRoles; } - @Override @Transactional(rollbackFor = Exception.class) public void assignRoles(SysUserRequest sysUserRequest) { @@ -128,6 +142,9 @@ public class SysUserRoleServiceImpl extends ServiceImpl sysUserCacheOperatorApi() { - TimedCache sysUserTimedCache = CacheUtil.newTimedCache(SystemConstants.USER_CACHE_TIMEOUT_SECONDS * 1000); + TimedCache sysUserTimedCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000); return new SysUserMemoryCache(sysUserTimedCache); } + /** + * 用户角色对应的缓存 + * + * @author fengshuonan + * @date 2021/7/29 23:00 + */ + @Bean + @ConditionalOnMissingBean(name = "userRoleCacheApi") + public CacheOperatorApi> userRoleCacheApi() { + TimedCache> sysUserTimedCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000); + return new UserRoleMemoryCache(sysUserTimedCache); + } + }