LRUMap
非线程安全
private LRUMap userCache = new LRUMap(50);
//根据token调用ldap服务获取用户信息
@Override
public Result getUserInfo(String token) {
HttpHeaders headers = new HttpHeaders();
//exchange是调用接口返回的用户数据
Result result = exchange.getBody();
// ResultCode.SUCCESS.getCode() 10000
if (result.getCode() != ResultCode.SUCCESS.getCode()){
//获取用户信息失败
return result;
}
//成功需要将用户信息保存到数据库
LinkedHashMap<String, String> ldapUserDto = (LinkedHashMap<String, String>) (result.getData());
User user = getUser(ldapUserDto);
if (user.getId() == null){
//不存在 插入
userMapper.insert(user);
}else {
//存在即更新
userMapper.updateById(user);
}
//将token 和用户信息放入缓存
userCache.put(token,user);
userService.updateUserCache(user.getId());
//获取用户组信息 将用户所有的组查出来
List<Map<String,Object>> groups = userMapper.selectUserGroupByName(user.getId());
UserInfoVo userInfoVo = userTransform.pojoToInfoVo(user);
userInfoVo.setGroups(groups);
return new Result(ResultCode.SUCCESS,userInfoVo);
}
@Override
public void updateUserCache(Long userId) {
User user = userMapper.selectById(userId);
userCache.put(userId,
user);
}