好友共同关注

点进个人主页能查看到你和你好友共同关注的人

利用Redis中的Set中的sinter求两个集合的交集

好友共同关注_第1张图片

//共同关注
@Override
public Result followCommons(Long id) {
    //1.查询当前用户登录的id
    UserDTO user = UserHolder.getUser();
    if(user==null){
        return Result.fail("请先登录");
    }
    Long userId = user.getId();
    String key="follow:"+userId;
    //2.做交集
    String key2="follow:"+id;
    Set intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
    if(intersect==null || intersect.isEmpty()){
        return Result.fail("不存在共同关注");
    }
    //3.解析id
    List ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
    //4.通过id查询用户并且返回用户信息
    List users = userService.listByIds(ids)
            .stream()
            .map(use -> (BeanUtil.copyProperties(use, UserDTO.class)))
            .collect(Collectors.toList());
    return Result.ok(users);
}

 

你可能感兴趣的:(Redis,springboot,java,数据库,redis)