mirror of https://gitee.com/stylefeng/roses
【7.1.1】【user】更新用户角色的缓存
parent
2c109e4ad6
commit
04aba23f58
|
@ -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:";
|
||||
|
||||
}
|
|
@ -57,16 +57,6 @@ public interface SystemConstants {
|
|||
*/
|
||||
String DEFAULT_SYSTEM_NAME = "Guns快速开发平台";
|
||||
|
||||
/**
|
||||
* 用户缓存的前缀
|
||||
*/
|
||||
String USER_CACHE_PREFIX = "user:";
|
||||
|
||||
/**
|
||||
* 用户缓存过期时间(1小时)
|
||||
*/
|
||||
Long USER_CACHE_TIMEOUT_SECONDS = 3600L;
|
||||
|
||||
/**
|
||||
* 超级管理员的角色编码
|
||||
*/
|
||||
|
|
|
@ -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<SysUserDTO>
|
|||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return SystemConstants.USER_CACHE_PREFIX;
|
||||
return SystemCachesConstants.USER_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<SysUserDTO> {
|
|||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return SystemConstants.USER_CACHE_PREFIX;
|
||||
return SystemCachesConstants.USER_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* 用户角色的内存缓存
|
||||
* <p>
|
||||
* key为userId,value是Long类型集合,为角色的集合
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/7/29 22:54
|
||||
*/
|
||||
public class UserRoleMemoryCache extends AbstractMemoryCacheOperator<List<Long>> {
|
||||
|
||||
public UserRoleMemoryCache(TimedCache<String, List<Long>> timedCache) {
|
||||
super(timedCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return SystemCachesConstants.USER_ROLES_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
|
||||
|
||||
@Resource(name = "userRoleCacheApi")
|
||||
private CacheOperatorApi<List<Long>> userRoleCacheApi;
|
||||
|
||||
@Override
|
||||
public void add(UserRoleRequest userRoleRequest) {
|
||||
|
@ -99,13 +103,23 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
|
|||
@Override
|
||||
|
||||
public List<Long> findRoleIdsByUserId(Long userId) {
|
||||
|
||||
// 先从缓存获取用户绑定的角色
|
||||
String key = String.valueOf(userId);
|
||||
List<Long> userRolesCache = userRoleCacheApi.get(key);
|
||||
if (userRolesCache != null) {
|
||||
return userRolesCache;
|
||||
}
|
||||
|
||||
// 从数据库查询角色
|
||||
UserRoleRequest userRoleRequest = new UserRoleRequest();
|
||||
userRoleRequest.setUserId(userId);
|
||||
List<SysUserRole> sysUserRoleList = this.findList(userRoleRequest);
|
||||
return sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
|
||||
List<Long> 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<SysUserRoleMapper, SysUs
|
|||
});
|
||||
|
||||
this.saveBatch(sysUserRoleList);
|
||||
|
||||
// 清除用户角色的缓存
|
||||
userRoleCacheApi.remove(String.valueOf(userId));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -27,13 +27,16 @@ package cn.stylefeng.roses.kernel.system.starter;
|
|||
import cn.hutool.cache.CacheUtil;
|
||||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
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 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;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户缓存的自动配置
|
||||
*
|
||||
|
@ -52,8 +55,21 @@ public class GunsUserCacheAutoConfiguration {
|
|||
@Bean
|
||||
@ConditionalOnMissingBean(name = "sysUserCacheOperatorApi")
|
||||
public CacheOperatorApi<SysUserDTO> sysUserCacheOperatorApi() {
|
||||
TimedCache<String, SysUserDTO> sysUserTimedCache = CacheUtil.newTimedCache(SystemConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
|
||||
TimedCache<String, SysUserDTO> 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<List<Long>> userRoleCacheApi() {
|
||||
TimedCache<String, List<Long>> sysUserTimedCache = CacheUtil.newTimedCache(SystemCachesConstants.USER_CACHE_TIMEOUT_SECONDS * 1000);
|
||||
return new UserRoleMemoryCache(sysUserTimedCache);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue