微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn)好友關(guān)注功能
?引言
本博文參考 黑馬 程序員B站 Redis課程系列
在點(diǎn)評項(xiàng)目中,有這樣的需求,如何實(shí)現(xiàn)筆記的好友關(guān)注、以及發(fā)布筆記后推送消息功能?
使用Redis 的 好友關(guān)注、以及發(fā)布筆記后推送消息功能
一、Redis 實(shí)現(xiàn)好友關(guān)注 – 關(guān)注與取消關(guān)注
需求:針對用戶的操作,可以對用戶進(jìn)行關(guān)注和取消關(guān)注功能。
在探店圖文的詳情頁面中,可以關(guān)注發(fā)布筆記的作者

具體實(shí)現(xiàn)思路:基于該表數(shù)據(jù)結(jié)構(gòu),實(shí)現(xiàn)2個接口
- 關(guān)注和取關(guān)接口
- 判斷是否關(guān)注的接口
關(guān)注是用戶之間的關(guān)系,是博主與粉絲的關(guān)系,數(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 '關(guān)聯(lián)的用戶id', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間', PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;
id為自增長,簡化開發(fā)
核心代碼
FollowController
//關(guān)注
@PutMapping("/{id}/{isFollow}")
public Result follow(@PathVariable("id") Long followUserId, @PathVariable("isFollow") Boolean isFollow) {
return followService.follow(followUserId, isFollow);
}
//取消關(guān)注
@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.判斷到底是關(guān)注還是取關(guān)
if (isFollow) {
// 2.關(guān)注,新增數(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.取關(guān),刪除 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) {
// 把關(guān)注用戶的id從Redis集合中移除
stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
}
}
return Result.ok();
}
@Override
public Result isFollow(Long followUserId) {
// 1.獲取登錄用戶
Long userId = UserHolder.getUser().getId();
// 2.查詢是否關(guān)注 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);
}
代碼編寫完畢,進(jìn)行測試
代碼測試
點(diǎn)擊進(jìn)行關(guān)注用戶

關(guān)注成功
取消關(guān)注

測試成功
二、Redis 實(shí)現(xiàn)好友關(guān)注 – 共同關(guān)注功能
實(shí)現(xiàn)共同關(guān)注好友功能,首先,需要進(jìn)入博主發(fā)布的指定筆記頁,然后點(diǎn)擊博主的頭像去查看詳細(xì)信息

核心代碼如下
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));
// 獲取當(dāng)前頁數(shù)據(jù)
List<Blog> records = page.getRecords();
return Result.ok(records);
}那么如何實(shí)現(xiàn)共同好友關(guān)注功能呢?
需求:利用Redis中的數(shù)據(jù)結(jié)構(gòu),實(shí)現(xiàn)共同關(guān)注功能。 在博主個人頁面展示出當(dāng)前用戶與博主的共同關(guān)注
思路分析: 使用Redis的Set集合實(shí)現(xiàn),我們把兩人關(guān)注的人分別放入到一個Set集合中,然后再通過API去查看兩個Set集合中的交集數(shù)據(jù)

改造核心代碼:
當(dāng)用戶關(guān)注某位用戶后,需要將數(shù)據(jù)存入Redis集合中,方便后續(xù)進(jìn)行共同關(guān)注的實(shí)現(xiàn),同時(shí)取消關(guān)注時(shí),需要刪除Redis中的集合
FlowServiceImpl
@Override
public Result follow(Long followUserId, Boolean isFollow) {
// 1.獲取登錄用戶
Long userId = UserHolder.getUser().getId();
String key = "follows:" + userId;
// 1.判斷到底是關(guān)注還是取關(guān)
if (isFollow) {
// 2.關(guān)注,新增數(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.取關(guān),刪除 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) {
// 把關(guān)注用戶的id從Redis集合中移除
stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
}
}
return Result.ok();
}
// 具體獲取好友共同關(guān)注代碼
@Override
public Result followCommons(Long id) {
// 1.獲取當(dāng)前用戶
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);
}
進(jìn)行測試

?小結(jié)
以上就是【Bug 終結(jié)者】對 微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn) 好友關(guān)注 的簡單介紹,Redis 實(shí)現(xiàn)好友關(guān)注功能也是 利用Set集合、ZSet集合實(shí)現(xiàn)這樣一個需求,同時(shí),采用Redis來實(shí)現(xiàn)更加的快速,減少系統(tǒng)的消耗,更加快速的實(shí)現(xiàn)數(shù)據(jù)展示! 下篇博文我們繼續(xù) 關(guān)注 如何 使用Redis 實(shí)現(xiàn)推送消息到粉絲收件箱以及滾動分頁查詢!
到此這篇關(guān)于微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn) 好友關(guān)注的文章就介紹到這了,更多相關(guān)Spring Boot 整合 Redis 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows環(huán)境下打開Redis閃退的解決方案
每次使用完Redis后,我們習(xí)慣性的動作是直接叉掉doc頁面,這樣導(dǎo)致的結(jié)果是Redis在后臺繼續(xù)運(yùn)行,沒有關(guān)閉,所以當(dāng)再次打開的時(shí)候直接閃退,文中有詳細(xì)的解決方案,需要的朋友可以參考下2024-03-03
Redis遠(yuǎn)程連接Redis客戶端的實(shí)現(xiàn)步驟
本文主要介紹了Redis遠(yuǎn)程連接Redis客戶端的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
redis的hGetAll函數(shù)的性能問題(記Redis那坑人的HGETALL)
這篇文章主要介紹了redis的hGetAll函數(shù)的性能問題,需要的朋友可以參考下2016-02-02

