【7.1.1】【user】更新用户角色的缓存

pull/22/head
fengshuonan 2021-07-29 23:24:29 +08:00
parent 2c109e4ad6
commit 04aba23f58
7 changed files with 145 additions and 20 deletions

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.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:";
}

View File

@ -57,16 +57,6 @@ public interface SystemConstants {
*/
String DEFAULT_SYSTEM_NAME = "Guns快速开发平台";
/**
*
*/
String USER_CACHE_PREFIX = "user:";
/**
* (1)
*/
Long USER_CACHE_TIMEOUT_SECONDS = 3600L;
/**
*
*/

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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.
*
* 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.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>
* keyuserIdvalueLong
*
* @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;
}
}

View File

@ -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));
}
/**

View File

@ -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);
}
}