@Test
void testGetAll() {
List<User> users = userService.list();
users.forEach(
user -> {
// 7.1,随机生成token,作为登录令牌
String token = UUID.randomUUID().toString(true);
// 7.2,将User对象转化为HashMap存储
UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
File file = new File("你要生成的文件地址");
FileOutputStream output = null;
try {
output = new FileOutputStream(file, true);
byte[] bytes = token.getBytes();
output.write(bytes);
output.write("\r\n".getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
output.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Map<String, Object> userMap = BeanUtil.beanToMap(userDTO, new HashMap<>(),
CopyOptions.create()
.setIgnoreNullValue(true)
.setFieldValueEditor((fieldName, fieldValue) -> fieldValue.toString()));
// 7.3,存储
String tokenKey = LOGIN_USER_KEY + token;
stringRedisTemplate.opsForHash().putAll(tokenKey, userMap);
// 7.4,设置token有效期
stringRedisTemplate.expire(tokenKey, LOGIN_USER_TTL, TimeUnit.MINUTES);
}
);
黑马Redis课程P691000条token生成代码
最新推荐文章于 2024-07-07 22:23:07 发布