读写分离

This commit is contained in:
2025-05-06 15:55:55 +08:00
parent 34d66ae908
commit eab99227c5
7 changed files with 18 additions and 18 deletions
@@ -2,7 +2,7 @@ package cn.odboy.core.cache.api;
import cn.odboy.core.service.system.dto.UserJwtVo; import cn.odboy.core.service.system.dto.UserJwtVo;
public interface SystemUserCacheApi { public interface SystemUserJwtApi {
/** /**
* 返回用户缓存 * 返回用户缓存
* *
@@ -1,6 +1,6 @@
package cn.odboy.core.cache.api.impl; package cn.odboy.core.cache.api.impl;
import cn.odboy.core.cache.api.SystemUserCacheApi; import cn.odboy.core.cache.api.SystemUserJwtApi;
import cn.odboy.core.constant.SystemRedisKey; import cn.odboy.core.constant.SystemRedisKey;
import cn.odboy.core.service.system.dto.UserJwtVo; import cn.odboy.core.service.system.dto.UserJwtVo;
import cn.odboy.redis.RedisHelper; import cn.odboy.redis.RedisHelper;
@@ -11,7 +11,7 @@ import javax.annotation.Resource;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class SystemUserCacheApiImpl implements SystemUserCacheApi { public class SystemUserJwtApiImpl implements SystemUserJwtApi {
@Resource @Resource
private RedisHelper redisHelper; private RedisHelper redisHelper;
@@ -3,7 +3,7 @@ package cn.odboy.core.cache.service;
import cn.odboy.core.service.system.dto.UserJwtVo; import cn.odboy.core.service.system.dto.UserJwtVo;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
public interface SystemUserCacheService { public interface SystemUserJwtService {
/** /**
* 添加缓存到Redis * 添加缓存到Redis
* *
@@ -3,7 +3,7 @@ package cn.odboy.core.cache.service.impl;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.odboy.core.constant.SystemRedisKey; import cn.odboy.core.constant.SystemRedisKey;
import cn.odboy.core.service.system.dto.UserJwtVo; import cn.odboy.core.service.system.dto.UserJwtVo;
import cn.odboy.core.cache.service.SystemUserCacheService; import cn.odboy.core.cache.service.SystemUserJwtService;
import cn.odboy.redis.RedisHelper; import cn.odboy.redis.RedisHelper;
import cn.odboy.util.StringUtil; import cn.odboy.util.StringUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -13,7 +13,7 @@ import javax.annotation.Resource;
* 用户缓存管理 * 用户缓存管理
*/ */
@Service @Service
public class SystemUserCacheServiceImpl implements SystemUserCacheService { public class SystemUserJwtServiceImpl implements SystemUserJwtService {
@Resource @Resource
private RedisHelper redisHelper; private RedisHelper redisHelper;
@@ -3,9 +3,9 @@ package cn.odboy.core.framework.permission;
import cn.odboy.core.api.system.SystemDataApi; import cn.odboy.core.api.system.SystemDataApi;
import cn.odboy.core.api.system.SystemRoleApi; import cn.odboy.core.api.system.SystemRoleApi;
import cn.odboy.core.api.system.SystemUserApi; import cn.odboy.core.api.system.SystemUserApi;
import cn.odboy.core.cache.api.SystemUserCacheApi; import cn.odboy.core.cache.api.SystemUserJwtApi;
import cn.odboy.core.dal.dataobject.system.User; import cn.odboy.core.dal.dataobject.system.User;
import cn.odboy.core.cache.service.SystemUserCacheService; import cn.odboy.core.cache.service.SystemUserJwtService;
import cn.odboy.core.service.system.dto.RoleCodeVo; import cn.odboy.core.service.system.dto.RoleCodeVo;
import cn.odboy.core.service.system.dto.UserJwtVo; import cn.odboy.core.service.system.dto.UserJwtVo;
import cn.odboy.exception.BadRequestException; import cn.odboy.exception.BadRequestException;
@@ -22,12 +22,12 @@ public class UserDetailsServiceImpl implements UserDetailsService {
private final SystemRoleApi systemRoleApi; private final SystemRoleApi systemRoleApi;
private final SystemUserApi systemUserApi; private final SystemUserApi systemUserApi;
private final SystemDataApi systemDataApi; private final SystemDataApi systemDataApi;
private final SystemUserCacheService systemUserCacheService; private final SystemUserJwtService systemUserJwtService;
private final SystemUserCacheApi systemUserCacheApi; private final SystemUserJwtApi systemUserJwtApi;
@Override @Override
public UserJwtVo loadUserByUsername(String username) { public UserJwtVo loadUserByUsername(String username) {
UserJwtVo userJwtVo = systemUserCacheApi.describeUserJwtModelByUsername(username); UserJwtVo userJwtVo = systemUserJwtApi.describeUserJwtModelByUsername(username);
if (userJwtVo == null) { if (userJwtVo == null) {
User user = systemUserApi.describeUserByUsername(username); User user = systemUserApi.describeUserByUsername(username);
if (user == null) { if (user == null) {
@@ -41,7 +41,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
// 初始化JwtUserDto // 初始化JwtUserDto
userJwtVo = new UserJwtVo(user, systemDataApi.describeDeptIdListByUserIdWithDeptId(user), authorities); userJwtVo = new UserJwtVo(user, systemDataApi.describeDeptIdListByUserIdWithDeptId(user), authorities);
// 添加缓存数据 // 添加缓存数据
systemUserCacheService.saveUserJwtModelByUserName(username, userJwtVo); systemUserJwtService.saveUserJwtModelByUserName(username, userJwtVo);
} }
} }
return userJwtVo; return userJwtVo;
@@ -11,7 +11,7 @@ import cn.odboy.core.dal.mysql.system.RoleMapper;
import cn.odboy.core.dal.mysql.system.RoleMenuMapper; import cn.odboy.core.dal.mysql.system.RoleMenuMapper;
import cn.odboy.core.dal.mysql.system.UserMapper; import cn.odboy.core.dal.mysql.system.UserMapper;
import cn.odboy.core.service.system.SystemRoleService; import cn.odboy.core.service.system.SystemRoleService;
import cn.odboy.core.cache.service.SystemUserCacheService; import cn.odboy.core.cache.service.SystemUserJwtService;
import cn.odboy.core.service.system.dto.CreateRoleRequest; import cn.odboy.core.service.system.dto.CreateRoleRequest;
import cn.odboy.exception.EntityExistException; import cn.odboy.exception.EntityExistException;
import cn.odboy.redis.RedisHelper; import cn.odboy.redis.RedisHelper;
@@ -36,7 +36,7 @@ public class SystemRoleServiceImpl extends ServiceImpl<RoleMapper, Role> impleme
private final RoleDeptMapper roleDeptMapper; private final RoleDeptMapper roleDeptMapper;
private final RoleMenuMapper roleMenuMapper; private final RoleMenuMapper roleMenuMapper;
private final UserMapper userMapper; private final UserMapper userMapper;
private final SystemUserCacheService systemUserCacheService; private final SystemUserJwtService systemUserJwtService;
private final RedisHelper redisHelper; private final RedisHelper redisHelper;
@Override @Override
@@ -125,7 +125,7 @@ public class SystemRoleServiceImpl extends ServiceImpl<RoleMapper, Role> impleme
public void delCaches(Long id, List<User> users) { public void delCaches(Long id, List<User> users) {
users = CollectionUtil.isEmpty(users) ? userMapper.queryUserListByRoleId(id) : users; users = CollectionUtil.isEmpty(users) ? userMapper.queryUserListByRoleId(id) : users;
if (CollectionUtil.isNotEmpty(users)) { if (CollectionUtil.isNotEmpty(users)) {
users.forEach(item -> systemUserCacheService.cleanUserJwtModelCacheByUsername(item.getUsername())); users.forEach(item -> systemUserJwtService.cleanUserJwtModelCacheByUsername(item.getUsername()));
Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet()); Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
redisHelper.delByKeys(SystemRedisKey.DATA_USER, userIds); redisHelper.delByKeys(SystemRedisKey.DATA_USER, userIds);
redisHelper.delByKeys(SystemRedisKey.MENU_USER, userIds); redisHelper.delByKeys(SystemRedisKey.MENU_USER, userIds);
@@ -9,7 +9,7 @@ import cn.odboy.core.dal.mysql.system.UserMapper;
import cn.odboy.core.dal.mysql.system.UserRoleMapper; import cn.odboy.core.dal.mysql.system.UserRoleMapper;
import cn.odboy.core.framework.permission.util.SecurityHelper; import cn.odboy.core.framework.permission.util.SecurityHelper;
import cn.odboy.core.framework.properties.AppProperties; import cn.odboy.core.framework.properties.AppProperties;
import cn.odboy.core.cache.service.SystemUserCacheService; import cn.odboy.core.cache.service.SystemUserJwtService;
import cn.odboy.core.cache.service.SystemUserOnlineService; import cn.odboy.core.cache.service.SystemUserOnlineService;
import cn.odboy.core.service.system.SystemUserService; import cn.odboy.core.service.system.SystemUserService;
import cn.odboy.exception.BadRequestException; import cn.odboy.exception.BadRequestException;
@@ -44,7 +44,7 @@ public class SystemUserServiceImpl extends ServiceImpl<UserMapper, User> impleme
private final UserRoleMapper userRoleMapper; private final UserRoleMapper userRoleMapper;
private final AppProperties properties; private final AppProperties properties;
private final RedisHelper redisHelper; private final RedisHelper redisHelper;
private final SystemUserCacheService systemUserCacheService; private final SystemUserJwtService systemUserJwtService;
private final SystemUserOnlineService systemUserOnlineService; private final SystemUserOnlineService systemUserOnlineService;
@Override @Override
@@ -242,6 +242,6 @@ public class SystemUserServiceImpl extends ServiceImpl<UserMapper, User> impleme
* @param username / * @param username /
*/ */
private void flushCache(String username) { private void flushCache(String username) {
systemUserCacheService.cleanUserJwtModelCacheByUsername(username); systemUserJwtService.cleanUserJwtModelCacheByUsername(username);
} }
} }