3.用户新增关注/取消关注这里比较简单,新增/取消关注,把新关注的 Feed 往自己的 Feed流中增加/删除 即可,但是同样需要异步处理 。
/** * 关注/取关 时,处理followerId的zSet * * @param followId被关注的人 * @param followerId 当前用户 * @param typefocus = 关注 unfocus = 取关 */public void handleFocus( Long followId, Long followerId, String type) {String focusFeedKey = "focusFeedKey" + userId;// 如果粉丝 zSet 不存在,退出if (!this.zSetRedisTemplate.exists(focusFeedKey)) {return;}List<FeedDto> FeedDtos = this.listFeedByFollowId(source, followId);for (FeedDto feedDto : FeedDtos) {// 关注if ("focus".equals(type)) {this.removeOldestZset(focusFeedKey);this.zSetRedisTemplate.zadd(focusFeedKey, feedDto.getCreateTime().getTime(), feedDto.getId());}// 取关else if ("unfocus".equals(type)) {this.zSetRedisTemplate.zrem(focusFeedKey, feedDto.getId());}}}上面展示的是核心代码,仅仅是为大家提供一个实现思路,并不是直接可运行的代码,毕竟真正实现还会涉及到很多其他的无关要紧的类 。
【巧用 Redis,实现微博 Feed 流功能!】
推荐阅读
- 苹果手机轻松实现微信双开分身
- Redis内存碎片:深度解析与优化策略
- SpringBoot如何实现热部署?
- Redis中的三种特殊类型
- 桶排序:原理、性能分析与 Java 实现
- Redis中的Big Key问题:排查与解决思路
- 如何去除冰箱异味? 如何去除冰箱异味
- 基于牛顿求根法,新算法实现并行训练和评估RNN,带来超10倍增速
- CORS 跨域资源共享在Spring Boot中的实现
- 使用Beacon API实现高效数据传输和用户行为分析
