mirror of https://gitee.com/stylefeng/roses
【8.0】【sys】【user】更新用户角色的缓存
parent
270ff4e6a8
commit
c9a3c8b3d2
|
@ -0,0 +1 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.user.cache;
|
|
@ -0,0 +1,26 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole;
|
||||
|
||||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户绑定角色的缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 22:00
|
||||
*/
|
||||
public class UserRoleMemoryCache extends AbstractMemoryCacheOperator<List<Long>> {
|
||||
|
||||
public UserRoleMemoryCache(TimedCache<String, List<Long>> timedCache) {
|
||||
super(timedCache);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return UserConstants.USER_ROLE_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole;
|
||||
|
||||
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户绑定角色的缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 21:58
|
||||
*/
|
||||
public class UserRoleRedisCache extends AbstractRedisCacheOperator<List<Long>> {
|
||||
|
||||
public UserRoleRedisCache(RedisTemplate<String, List<Long>> redisTemplate) {
|
||||
super(redisTemplate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return UserConstants.USER_ROLE_CACHE_PREFIX;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.user.cache.userrole.clear;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.event.api.annotation.BusinessListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants.UPDATE_USER_ROLE_EVENT;
|
||||
|
||||
/**
|
||||
* 监听用户绑定角色的事件,清空相关缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 22:05
|
||||
*/
|
||||
@Service
|
||||
public class UserRoleClearListener {
|
||||
|
||||
@Resource(name = "userRoleCache")
|
||||
private CacheOperatorApi<List<Long>> userRoleCache;
|
||||
|
||||
/**
|
||||
* 监听更新用户角色
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 22:10
|
||||
*/
|
||||
@BusinessListener(businessCode = UPDATE_USER_ROLE_EVENT)
|
||||
public void updateUserRole(Long userId) {
|
||||
if (ObjectUtil.isNotEmpty(userId)) {
|
||||
userRoleCache.remove(String.valueOf(userId));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.user.constants;
|
||||
|
||||
/**
|
||||
* 用户相关的常量
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/7/14 22:06
|
||||
*/
|
||||
public interface UserConstants {
|
||||
|
||||
/**
|
||||
* 缓存前缀:用户绑定的角色
|
||||
*/
|
||||
String USER_ROLE_CACHE_PREFIX = "SYS:USER_ROLE:";
|
||||
|
||||
/**
|
||||
* 修改用户绑定角色的事件
|
||||
*/
|
||||
String UPDATE_USER_ROLE_EVENT = "UPDATE_USER_ROLE_EVENT";
|
||||
|
||||
}
|
|
@ -1,13 +1,10 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.user.service;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
|
||||
import cn.stylefeng.roses.kernel.sys.api.SysUserRoleServiceApi;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.pojo.request.SysUserRoleRequest;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户角色关联 服务类
|
||||
*
|
||||
|
@ -16,62 +13,6 @@ import java.util.List;
|
|||
*/
|
||||
public interface SysUserRoleService extends IService<SysUserRole>, SysUserRoleServiceApi {
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param sysUserRoleRequest 请求参数
|
||||
* @author fengshuonan
|
||||
* @date 2023/06/10 21:26
|
||||
*/
|
||||
void add(SysUserRoleRequest sysUserRoleRequest);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param sysUserRoleRequest 请求参数
|
||||
* @author fengshuonan
|
||||
* @date 2023/06/10 21:26
|
||||
*/
|
||||
void del(SysUserRoleRequest sysUserRoleRequest);
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*
|
||||
* @param sysUserRoleRequest 请求参数
|
||||
* @author fengshuonan
|
||||
* @date 2023/06/10 21:26
|
||||
*/
|
||||
void edit(SysUserRoleRequest sysUserRoleRequest);
|
||||
|
||||
/**
|
||||
* 查询详情
|
||||
*
|
||||
* @param sysUserRoleRequest 请求参数
|
||||
* @author fengshuonan
|
||||
* @date 2023/06/10 21:26
|
||||
*/
|
||||
SysUserRole detail(SysUserRoleRequest sysUserRoleRequest);
|
||||
|
||||
/**
|
||||
* 获取列表
|
||||
*
|
||||
* @param sysUserRoleRequest 请求参数
|
||||
* @return List<SysUserRole> 返回结果
|
||||
* @author fengshuonan
|
||||
* @date 2023/06/10 21:26
|
||||
*/
|
||||
List<SysUserRole> findList(SysUserRoleRequest sysUserRoleRequest);
|
||||
|
||||
/**
|
||||
* 获取列表(带分页)
|
||||
*
|
||||
* @param sysUserRoleRequest 请求参数
|
||||
* @return PageResult<SysUserRole> 返回结果
|
||||
* @author fengshuonan
|
||||
* @date 2023/06/10 21:26
|
||||
*/
|
||||
PageResult<SysUserRole> findPage(SysUserRoleRequest sysUserRoleRequest);
|
||||
|
||||
/**
|
||||
* 绑定用户角色
|
||||
*
|
||||
|
|
|
@ -1,23 +1,19 @@
|
|||
package cn.stylefeng.roses.kernel.sys.modular.user.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
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;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.event.sdk.publish.BusinessEventPublisher;
|
||||
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
|
||||
import cn.stylefeng.roses.kernel.sys.api.SysRoleServiceApi;
|
||||
import cn.stylefeng.roses.kernel.sys.api.callback.RemoveRoleCallbackApi;
|
||||
import cn.stylefeng.roses.kernel.sys.api.callback.RemoveUserCallbackApi;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.entity.SysUserRole;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.enums.SysUserExceptionEnum;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.enums.SysUserRoleExceptionEnum;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.mapper.SysUserRoleMapper;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.pojo.request.SysUserRoleRequest;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserRoleService;
|
||||
import cn.stylefeng.roses.kernel.sys.modular.user.service.SysUserService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
@ -28,6 +24,8 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.stylefeng.roses.kernel.sys.modular.user.constants.UserConstants.UPDATE_USER_ROLE_EVENT;
|
||||
|
||||
/**
|
||||
* 用户角色关联业务实现层
|
||||
*
|
||||
|
@ -44,37 +42,8 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
|
|||
@Resource
|
||||
private SysUserService sysUserService;
|
||||
|
||||
@Override
|
||||
public void add(SysUserRoleRequest sysUserRoleRequest) {
|
||||
SysUserRole sysUserRole = new SysUserRole();
|
||||
BeanUtil.copyProperties(sysUserRoleRequest, sysUserRole);
|
||||
this.save(sysUserRole);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void del(SysUserRoleRequest sysUserRoleRequest) {
|
||||
SysUserRole sysUserRole = this.querySysUserRole(sysUserRoleRequest);
|
||||
this.removeById(sysUserRole.getUserRoleId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void edit(SysUserRoleRequest sysUserRoleRequest) {
|
||||
SysUserRole sysUserRole = this.querySysUserRole(sysUserRoleRequest);
|
||||
BeanUtil.copyProperties(sysUserRoleRequest, sysUserRole);
|
||||
this.updateById(sysUserRole);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUserRole detail(SysUserRoleRequest sysUserRoleRequest) {
|
||||
return this.querySysUserRole(sysUserRoleRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<SysUserRole> findPage(SysUserRoleRequest sysUserRoleRequest) {
|
||||
LambdaQueryWrapper<SysUserRole> wrapper = createWrapper(sysUserRoleRequest);
|
||||
Page<SysUserRole> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
|
||||
return PageResultFactory.createPageResult(sysRolePage);
|
||||
}
|
||||
@Resource(name = "userRoleCache")
|
||||
private CacheOperatorApi<List<Long>> userRoleCache;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
@ -100,6 +69,10 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
|
|||
newUserRoles.add(sysUserRole);
|
||||
}
|
||||
this.saveBatch(newUserRoles);
|
||||
|
||||
// 发布修改用户绑定角色的事件
|
||||
BusinessEventPublisher.publishEvent(UPDATE_USER_ROLE_EVENT, sysUserRoleRequest.getUserId());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -113,12 +86,10 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
|
|||
sysUserRole.setUserId(userId);
|
||||
sysUserRole.setRoleId(defaultRoleId);
|
||||
this.save(sysUserRole);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysUserRole> findList(SysUserRoleRequest sysUserRoleRequest) {
|
||||
LambdaQueryWrapper<SysUserRole> wrapper = this.createWrapper(sysUserRoleRequest);
|
||||
return this.list(wrapper);
|
||||
// 发布修改用户绑定角色的事件
|
||||
BusinessEventPublisher.publishEvent(UPDATE_USER_ROLE_EVENT, userId);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -151,26 +122,26 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
|
|||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 先从缓存查找用户的角色
|
||||
List<Long> cachedRoleIds = userRoleCache.get(userId.toString());
|
||||
if (cachedRoleIds != null) {
|
||||
return cachedRoleIds;
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(SysUserRole::getUserId, userId);
|
||||
wrapper.select(SysUserRole::getRoleId);
|
||||
List<SysUserRole> sysUserRoleList = this.list(wrapper);
|
||||
|
||||
return sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
|
||||
}
|
||||
List<Long> userRoleQueryResult = sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
|
||||
|
||||
/**
|
||||
* 获取信息
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2023/06/10 21:26
|
||||
*/
|
||||
private SysUserRole querySysUserRole(SysUserRoleRequest sysUserRoleRequest) {
|
||||
SysUserRole sysUserRole = this.getById(sysUserRoleRequest.getUserRoleId());
|
||||
if (ObjectUtil.isEmpty(sysUserRole)) {
|
||||
throw new ServiceException(SysUserRoleExceptionEnum.SYS_USER_ROLE_NOT_EXISTED);
|
||||
// 查询结果缓存起来
|
||||
if (ObjectUtil.isNotEmpty(userRoleQueryResult)) {
|
||||
userRoleCache.put(userId.toString(), userRoleQueryResult);
|
||||
return userRoleQueryResult;
|
||||
}
|
||||
return sysUserRole;
|
||||
|
||||
return userRoleQueryResult;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue