基于Redis的微博关注与粉丝

基于Redis的微博关注与粉丝

一、微博关注与粉丝的业务场景分析

阿甘关注了雷军:阿甘就是雷军的粉丝follower
雷军被阿甘关注:雷军就是阿甘的关注followee

二、微博关注与粉丝的redis技术方案

技术方案:每个用户都有2个集合:关注集合和粉丝集合
例如 阿甘关注了雷军,做了2个动作

1.把阿甘的userid加入雷军的粉丝follower集合set
2.把雷军的userid加入阿甘的关注followee集合set
集合key设计
阿甘的关注集合 key=followee:阿甘userid
雷军的粉丝集合 key=follower:雷军userid

SpringBoot+Redis 实现微博关注与粉丝

功能1:关注
    /**
     * 阿甘关注了雷军,阿甘即使雷军的粉丝 follower
     * userId=阿甘
     * followeeId=雷军
     */
    @ApiOperation(value="关注")
    @PostMapping(value = "/follow")
    public void follow(Integer userId,Integer followeeId) {
        relationService.follow(userId,followeeId);
    }
    /**
     * 阿甘关注了雷军
     */
    public void follow(Integer userId,Integer followeeId){
        SetOperations<String, Integer> opsForSet = redisTemplate.opsForSet();
        //阿甘的关注集合
        String followeekey = Constants.CACHE_KEY_FOLLOWEE + userId;
        //把雷军的followeeId,加入阿甘的关注集合中
        opsForSet.add(followeekey,followeeId);

        //雷军的粉丝集合
        String followerkey=Constants.CACHE_KEY_FOLLOWER+followeeId;
        //把阿甘的userid加入雷军的粉丝follower集合set
        opsForSet.add(followerkey,userId);

    }
功能2:我的关注
    @ApiOperation(value="查看我的关注")
    @GetMapping(value = "/myFollowee")
    public List<UserVO> myFollowee(Integer userId){

        return this.relationService.myFollowee(userId);
    }
    
    /**
     * 查看我的关注
     */
    public List<UserVO> myFollowee(Integer userId){
        SetOperations<String, Integer> opsForSet = redisTemplate.opsForSet();
        //关注集合
        String followeekey = Constants.CACHE_KEY_FOLLOWEE + userId;
        Set<Integer> sets= opsForSet.members(followeekey);
        return this.getUserInfo(sets);
    }
功能3:我的粉丝
    @ApiOperation(value="查看我的粉丝")
    @GetMapping(value = "/myFollower")
    public List<UserVO> myFollower(Integer userId){
        return this.relationService.myFollower(userId);
    }
    /**
     * 查看我的粉丝
     */
    public List<UserVO> myFollower(Integer userId){
        SetOperations<String, Integer> opsForSet = redisTemplate.opsForSet();
        //粉丝集合
        String followerkey=Constants.CACHE_KEY_FOLLOWER+userId;
        Set<Integer> sets= opsForSet.members(followerkey);

        return this.getUserInfo(sets);
    }

三、体验效果

用户和id的数据库对应关系
阿甘->id=1
雷军->id=2
王石->id=3
潘石岂->id=4

关注顺序
阿甘关注:雷军 王石 潘石岂
雷军关注:王石 潘石岂
王石关注: 雷军
潘石岂关注:王石

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值