refactor(role): 使用 RedisTemplate 替换 StringRedisTemplate 优化角色数据同步

- 将 RoleServiceImpl 中 stringRedisTemplate 替换为更通用的 redisTemplate
- 调整角色和用户角色关系写入 Redis 的代码,删除存储过期时间参数
- 添加日志记录,输出同步到 Redis 的用户角色关系数据
- 删除 LoginServiceImpl 中未实现的 initUserRole 方法
- 新增 RoleService 的单元测试 RoleTest,验证角色权限同步功能
- 移除 UserRoleRelDOMapper.xml 中 selectAll 查询的 is_deleted 过滤条件
This commit is contained in:
lbw
2025-12-24 15:35:23 +08:00
parent 260c2c79f1
commit 15e909c318
4 changed files with 26 additions and 8 deletions

View File

@@ -80,10 +80,6 @@ public class LoginServiceImpl implements LoginService {
}
}
private void initUserRole(UserDO userDO) {
// todo
}
/**
* 发送短信
*/

View File

@@ -10,6 +10,7 @@ import com.yinlihupo.enlish.service.service.RoleService;
import com.yinlihupo.framework.common.util.JsonUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@@ -22,7 +23,7 @@ import java.util.stream.Collectors;
public class RoleServiceImpl implements RoleService {
@Resource
private StringRedisTemplate stringRedisTemplate;
private RedisTemplate<String, Object> redisTemplate;
@Resource
private RoleDOMapper roleDOMapper;
@Resource
@@ -34,11 +35,12 @@ public class RoleServiceImpl implements RoleService {
List<RoleDO> roleDOS = roleDOMapper.selectAll();
List<String> roleKeys = roleDOS.stream().map(RoleDO::getRoleKey).toList();
log.info("将角色同步到 redis 中, {}", roleKeys);
stringRedisTemplate.opsForValue().set(RoleConstants.ROLE, JsonUtils.toJsonString(roleKeys), 60 * 60 * 24);
redisTemplate.opsForValue().set(RoleConstants.ROLE, JsonUtils.toJsonString(roleKeys));
Map<Long, RoleDO> roleId2RoleDO = roleDOS.stream().collect(Collectors.toMap(RoleDO::getId, roleDO -> roleDO));
List<UserRoleRelDO> userRoleRelDOS = userRoleRelDOMapper.selectAll();
log.info("将用户角色关系同步到 redis 中, {}", userRoleRelDOS);
Map<Long, List<UserRoleRelDO>> userId2UserRoleRelDOs = userRoleRelDOS.stream().collect(Collectors.groupingBy(UserRoleRelDO::getUserId));
userId2UserRoleRelDOs.forEach((userId, userRoleRelDOs) -> {
@@ -46,7 +48,7 @@ public class RoleServiceImpl implements RoleService {
List<RoleDO> roleDOs = roleIds.stream().map(roleId2RoleDO::get).toList();
List<String> user2RoleKeys = roleDOs.stream().map(RoleDO::getRoleKey).toList();
log.info("将用户 {} 的角色同步到 redis 中, {}", userId, roleKeys);
stringRedisTemplate.opsForValue().set(RoleConstants.buildUserRoleKey(userId), JsonUtils.toJsonString(user2RoleKeys));
redisTemplate.opsForValue().set(RoleConstants.buildUserRoleKey(userId), JsonUtils.toJsonString(user2RoleKeys));
});
}

View File

@@ -11,7 +11,6 @@
<select id="selectAll" resultMap="BaseResultMap">
select *
from user_role_rel
where is_deleted = 0
</select>
<select id="selectByUserIds" resultMap="BaseResultMap">

View File

@@ -0,0 +1,21 @@
package com.yinlihupo.enlish.service.service.role;
import com.yinlihupo.enlish.service.service.RoleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@Slf4j
public class RoleTest {
@Resource
private RoleService roleService;
@Test
public void test() {
log.info("==> 测试");
roleService.pushRolePermission2Redis();
}
}