微服务Spring Boot 整合 Redis 实现 好友关注
文章目录
- ⛅引言
- 一、Redis 实现好友关注 -- 关注与取消关注
- 二、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);
}
代码编写完毕,进行测试
代码测试
点击进行关注用户
关注成功
取消关注
测试成功
二、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);
}
那么如何实现共同好友关注功能呢?
需求:利用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);
}
进行测试
⛵小结
以上就是【Bug 终结者】对 微服务Spring Boot 整合 Redis 实现 好友关注 的简单介绍,Redis 实现好友关注功能也是 利用Set集合、ZSet集合实现这样一个需求,同时,采用Redis来实现更加的快速,减少系统的消耗,更加快速的实现数据展示! 下篇博文我们继续 关注 如何 使用Redis 实现推送消息到粉丝收件箱以及滚动分页查询!
如果这篇【文章】有帮助到你,希望可以给【Bug 终结者】点个赞
微服务Spring Boot 整合 Redis 实现 好友关注相关推荐
- 猿创征文 | 微服务 Spring Boot 整合Redis 实战开发解决高并发数据缓存
文章目录 一.什么是 缓存? ⛅为什么用缓存? ⚡如何使用缓存 二.实现一个商家缓存 ⌛环境搭建 ♨️核心源码 ✅测试接口 三.采用 微服务 Spring Boot 注解开启缓存 ✂️@CacheEn ...
- Spring boot - 整合 Redis缓存(上)
一.配置Pom文件 在使用spring boot 2.0整合redis时遇到了好多问题,网上很多例子都是1.x版本的.故2.0没有折腾好所以将2.0降到了1.5.降级后由于thymeleaf版本也会从 ...
- Spring Boot基础学习笔记08:Spring Boot整合Redis
文章目录 零.学习目标 1.熟悉Redis相关概念 2.掌握使用Spring Boot整合Redis 一.Redis概述 1.Redis简介 2.Redis优点 (1)存取速度快 (2)数据类型丰富 ...
- 十一、Spring Boot整合Redis(一)
Spring Boot整合Redis 1. SpringBoot+单例Redis 1)引入依赖 <dependency> <groupId>org.springf ...
- Spring boot整合Redis(入门教程)
目录 源码分析 jedis VS lettuce 整合测试 导入依赖 配置连接 测试 存入字符串 存入对象 五大数据类型操作 自定义RedisConfig 存入对象 Redis工具类(常用API) 以 ...
- 大聪明教你学Java | Spring Boot 整合 Redis 实现访问量统计
前言 之前开发系统的时候客户提到了一个需求:需要统计某些页面的访问量,记得当时还纠结了一阵子,不知道怎么去实现这个功能,后来还是在大佬的带领下借助 Redis 实现了这个功能.今天又回想起了这件事,正 ...
- Spring Boot 整合Redis 包含Java操作Redis哨兵 作者:哇塞大嘴好帥(哇塞大嘴好帅)
Spring Boot 整合Redis 包含Java操作Redis哨兵 作者:哇塞大嘴好帥(哇塞大嘴好帅) 1. 配置环境 在SpringBoot2.0版本以后,原来使用的jedis被替换成为了let ...
- Spring boot整合Redis实现发布订阅(超详细)
Redis发布订阅 基础知识 相关命令 订阅者/等待接收消息 发布者/发送消息 订阅者/成功接收消息 常用命令汇总 原理 Spring boot整合redis 导入依赖 Redis配置 消息封装类(M ...
- [由零开始]Spring boot 整合redis集群
Spring boot 整合redis集群 一.环境搭建 Redis集群环境搭建:https://blog.csdn.net/qq497811258/article/details/108124697 ...
最新文章
- NumericUpDown
- Endnote X8云同步:家里单位实时同步文献笔记,有网随时读文献
- Nokia5110液晶屏完全新手学习笔记(二)
- 四. RxJava之基本原理
- OpenStack工作流服务Mistral简介
- 【题解】Luogu P5279 [ZJOI2019]麻将
- 滴滴司机端大更新并公布了一份设计方案!
- Html5和Css3扁平化风格网页
- MongoDB索引类型
- python编程快速上手自动化_《Python编程快速上手 让繁琐工作自动化》完整版PDF...
- 想要羊毛薅得少,欺诈防控少不了
- 详解语音识别的技术原理
- iOS 获取本地视频的缩略图
- HDFS HA机制 及 Secondary NameNode详解
- 使用mencoder或(ffm)将图片生成视频
- 阿里云Linux服务器部署JDK8实战教程
- python中使用requests库获取昵图网图片,且正则中re.S的用法
- 8086的两种工作模式_8086有哪两种工作模式?其主要区别是什么?
- oracle10G安装与配置
- Vue计算属性和函数的区别
热门文章