微服务SpringBoot整合Redis实现好友关注功能
“纵有疾风来,人生不言弃”,这句话送给正在学习数据库的朋友们,也希望在阅读本文《微服务SpringBoot整合Redis实现好友关注功能》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新数据库相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!
⛅引言
本博文参考 黑马 程序员B站 Redis课程系列
在点评项目中,有这样的需求,如何实现笔记的好友关注、以及发布笔记后推送消息功能?
使用Redis 的 好友关注、以及发布笔记后推送消息功能
一、Redis 实现好友关注 – 关注与取消关注
需求:针对用户的操作,可以对用户进行关注和取消关注功能。
在探店图文的详情页面中,可以关注发布笔记的作者
具体实现思路:基于该表数据结构,实现2个接口
- 关注和取关接口
- 判断是否关注的接口
关注是用户之间的关系,是博主与粉丝的关系,数据表如下:
tb_follow
CREATE TABLE `tb_follow` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `user_id` bigint(20) unsigned NOT NULL COMMENT '用户id', `follow_user_id` bigint(20) unsigned NOT NULL COMMENT '关联的用户id', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;
id为自增长,简化开发
核心代码
FollowController
//关注 @PutMapping("/{id}/{isFollow}") public Result follow(@PathVariable("id") Long followUserId, @PathVariable("isFollow") Boolean isFollow) { return followService.follow(followUserId, isFollow); } //取消关注 @GetMapping("/or/not/{id}") public Result isFollow(@PathVariable("id") Long followUserId) { return followService.isFollow(followUserId); }
FollowService
@Override public Result follow(Long followUserId, Boolean isFollow) { // 1.获取登录用户 Long userId = UserHolder.getUser().getId(); String key = "follows:" + userId; // 1.判断到底是关注还是取关 if (isFollow) { // 2.关注,新增数据 Follow follow = new Follow(); follow.setUserId(userId); follow.setFollowUserId(followUserId); boolean isSuccess = save(follow); if (isSuccess) { stringRedisTemplate.opsForSet().add(key, followUserId.toString()); } } else { // 3.取关,删除 delete from tb_follow where user_id = ? and follow_user_id = ? boolean isSuccess = remove(new QueryWrapper<follow>() .eq("user_id", userId).eq("follow_user_id", followUserId)); if (isSuccess) { // 把关注用户的id从Redis集合中移除 stringRedisTemplate.opsForSet().remove(key, followUserId.toString()); } } return Result.ok(); } @Override public Result isFollow(Long followUserId) { // 1.获取登录用户 Long userId = UserHolder.getUser().getId(); // 2.查询是否关注 select count(*) from tb_follow where user_id = ? and follow_user_id = ? Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count(); // 3.判断 return Result.ok(count > 0); } </follow>
代码编写完毕,进行测试
代码测试
点击进行关注用户
关注成功
取消关注
测试成功
二、Redis 实现好友关注 – 共同关注功能
实现共同关注好友功能,首先,需要进入博主发布的指定笔记页,然后点击博主的头像去查看详细信息
核心代码如下
UserController
// UserController 根据id查询用户 @GetMapping("/{id}") public Result queryUserById(@PathVariable("id") Long userId){ // 查询详情 User user = userService.getById(userId); if (user == null) { return Result.ok(); } UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class); // 返回 return Result.ok(userDTO); }
BlogController
@GetMapping("/of/user") public Result queryBlogByUserId( @RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam("id") Long id) { // 根据用户查询 Page<blog> page = blogService.query() .eq("user_id", id).page(new Page(current, SystemConstants.MAX_PAGE_SIZE)); // 获取当前页数据 List<blog> records = page.getRecords(); return Result.ok(records); }</blog></blog>
那么如何实现共同好友关注功能呢?
需求:利用Redis中的数据结构,实现共同关注功能。 在博主个人页面展示出当前用户与博主的共同关注
思路分析: 使用Redis的Set集合实现,我们把两人关注的人分别放入到一个Set集合中,然后再通过API去查看两个Set集合中的交集数据
改造核心代码:
当用户关注某位用户后,需要将数据存入Redis集合中,方便后续进行共同关注的实现,同时取消关注时,需要删除Redis中的集合
FlowServiceImpl
@Override public Result follow(Long followUserId, Boolean isFollow) { // 1.获取登录用户 Long userId = UserHolder.getUser().getId(); String key = "follows:" + userId; // 1.判断到底是关注还是取关 if (isFollow) { // 2.关注,新增数据 Follow follow = new Follow(); follow.setUserId(userId); follow.setFollowUserId(followUserId); boolean isSuccess = save(follow); if (isSuccess) { stringRedisTemplate.opsForSet().add(key, followUserId.toString()); } } else { // 3.取关,删除 delete from tb_follow where user_id = ? and follow_user_id = ? boolean isSuccess = remove(new QueryWrapper<follow>() .eq("user_id", userId).eq("follow_user_id", followUserId)); if (isSuccess) { // 把关注用户的id从Redis集合中移除 stringRedisTemplate.opsForSet().remove(key, followUserId.toString()); } } return Result.ok(); } // 具体获取好友共同关注代码 @Override public Result followCommons(Long id) { // 1.获取当前用户 Long userId = UserHolder.getUser().getId(); String key = "follows:" + userId; // 2.求交集 String key2 = "follows:" + id; Set<string> intersect = stringRedisTemplate.opsForSet().intersect(key, key2); if (intersect == null || intersect.isEmpty()) { // 无交集 return Result.ok(Collections.emptyList()); } // 3.解析id集合 List<long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList()); // 4.查询用户 List<userdto> users = userService.listByIds(ids) .stream() .map(user -> BeanUtil.copyProperties(user, UserDTO.class)) .collect(Collectors.toList()); return Result.ok(users); } </userdto></long></string></follow>
进行测试
⛵小结
好了,本文到此结束,带大家了解了《微服务SpringBoot整合Redis实现好友关注功能》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!

- 上一篇
- redis模糊批量删除key的方法

- 下一篇
- Redis中Bloomfilter布隆过滤器的学习
-
- 数据库 · Redis | 2天前 |
- Redis性能优化配置指南
- 182浏览 收藏
-
- 数据库 · Redis | 3天前 |
- RedisHyperLogLog大数据统计技巧
- 305浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis安全配置参数设置详解
- 252浏览 收藏
-
- 数据库 · Redis | 4天前 |
- 不同环境Redis安全配置对比与调整方法
- 374浏览 收藏
-
- 数据库 · Redis | 5天前 |
- RedisList队列优化方法分享
- 311浏览 收藏
-
- 数据库 · Redis | 5天前 |
- Redis主从复制故障排查指南
- 178浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis原子操作详解与实战应用
- 469浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis崩溃后重启与数据恢复方法
- 153浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis安全配置:强密码与访问控制设置教程
- 440浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- Redis单节点迁移集群的实用方法
- 376浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- 多线程Redis优化技巧分享
- 499浏览 收藏
-
- 数据库 · Redis | 1星期前 |
- RedisHyperLogLog高效统计方法
- 419浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 514次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 499次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- AI Mermaid流程图
- SEO AI Mermaid 流程图工具:基于 Mermaid 语法,AI 辅助,自然语言生成流程图,提升可视化创作效率,适用于开发者、产品经理、教育工作者。
- 493次使用
-
- 搜获客【笔记生成器】
- 搜获客笔记生成器,国内首个聚焦小红书医美垂类的AI文案工具。1500万爆款文案库,行业专属算法,助您高效创作合规、引流的医美笔记,提升运营效率,引爆小红书流量!
- 486次使用
-
- iTerms
- iTerms是一款专业的一站式法律AI工作台,提供AI合同审查、AI合同起草及AI法律问答服务。通过智能问答、深度思考与联网检索,助您高效检索法律法规与司法判例,告别传统模板,实现合同一键起草与在线编辑,大幅提升法律事务处理效率。
- 514次使用
-
- TokenPony
- TokenPony是讯盟科技旗下的AI大模型聚合API平台。通过统一接口接入DeepSeek、Kimi、Qwen等主流模型,支持1024K超长上下文,实现零配置、免部署、极速响应与高性价比的AI应用开发,助力专业用户轻松构建智能服务。
- 554次使用
-
- 迅捷AIPPT
- 迅捷AIPPT是一款高效AI智能PPT生成软件,一键智能生成精美演示文稿。内置海量专业模板、多样风格,支持自定义大纲,助您轻松制作高质量PPT,大幅节省时间。
- 483次使用
-
- 分享Redis高可用架构设计实践
- 2023-01-24 286浏览
-
- Go与Redis实现分布式互斥锁和红锁
- 2022-12-22 117浏览
-
- Redis的各项功能解决了哪些问题?
- 2023-02-18 185浏览
-
- Go+Redis实现延迟队列实操
- 2023-02-23 426浏览
-
- 分享 echo-framework 项目基础框架
- 2023-01-11 134浏览