微服務Spring Boot 整合 Redis 實現(xiàn)好友關注功能
?引言
本博文參考 黑馬 程序員B站 Redis課程系列
在點評項目中,有這樣的需求,如何實現(xiàn)筆記的好友關注、以及發(fā)布筆記后推送消息功能?
使用Redis 的 好友關注、以及發(fā)布筆記后推送消息功能
一、Redis 實現(xiàn)好友關注 – 關注與取消關注
需求:針對用戶的操作,可以對用戶進行關注和取消關注功能。
在探店圖文的詳情頁面中,可以關注發(fā)布筆記的作者
具體實現(xiàn)思路:基于該表數(shù)據(jù)結構,實現(xiàn)2個接口
- 關注和取關接口
- 判斷是否關注的接口
關注是用戶之間的關系,是博主與粉絲的關系,數(shù)據(jù)表如下:
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 '關聯(lián)的用戶id', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時間', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;
id為自增長,簡化開發(fā)
核心代碼
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.關注,新增數(shù)據(jù) 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 實現(xiàn)好友關注 – 共同關注功能
實現(xiàn)共同關注好友功能,首先,需要進入博主發(fā)布的指定筆記頁,然后點擊博主的頭像去查看詳細信息
核心代碼如下
UserController
// UserController 根據(jù)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) { // 根據(jù)用戶查詢 Page<Blog> page = blogService.query() .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE)); // 獲取當前頁數(shù)據(jù) List<Blog> records = page.getRecords(); return Result.ok(records); }
那么如何實現(xiàn)共同好友關注功能呢?
需求:利用Redis中的數(shù)據(jù)結構,實現(xiàn)共同關注功能。 在博主個人頁面展示出當前用戶與博主的共同關注
思路分析: 使用Redis的Set集合實現(xiàn),我們把兩人關注的人分別放入到一個Set集合中,然后再通過API去查看兩個Set集合中的交集數(shù)據(jù)
改造核心代碼:
當用戶關注某位用戶后,需要將數(shù)據(jù)存入Redis集合中,方便后續(xù)進行共同關注的實現(xiàn),同時取消關注時,需要刪除Redis中的集合
FlowServiceImpl
@Override public Result follow(Long followUserId, Boolean isFollow) { // 1.獲取登錄用戶 Long userId = UserHolder.getUser().getId(); String key = "follows:" + userId; // 1.判斷到底是關注還是取關 if (isFollow) { // 2.關注,新增數(shù)據(jù) 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 實現(xiàn) 好友關注 的簡單介紹,Redis 實現(xiàn)好友關注功能也是 利用Set集合、ZSet集合實現(xiàn)這樣一個需求,同時,采用Redis來實現(xiàn)更加的快速,減少系統(tǒng)的消耗,更加快速的實現(xiàn)數(shù)據(jù)展示! 下篇博文我們繼續(xù) 關注 如何 使用Redis 實現(xiàn)推送消息到粉絲收件箱以及滾動分頁查詢!
到此這篇關于微服務Spring Boot 整合 Redis 實現(xiàn) 好友關注的文章就介紹到這了,更多相關Spring Boot 整合 Redis 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Windows環(huán)境下打開Redis閃退的解決方案
每次使用完Redis后,我們習慣性的動作是直接叉掉doc頁面,這樣導致的結果是Redis在后臺繼續(xù)運行,沒有關閉,所以當再次打開的時候直接閃退,文中有詳細的解決方案,需要的朋友可以參考下2024-03-03redis的hGetAll函數(shù)的性能問題(記Redis那坑人的HGETALL)
這篇文章主要介紹了redis的hGetAll函數(shù)的性能問題,需要的朋友可以參考下2016-02-02