欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn)好友關(guān)注功能

 更新時(shí)間:2022年12月14日 10:59:30   作者:Bug 終結(jié)者  
這篇文章主要介紹了微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn) 好友關(guān)注,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

?引言

本博文參考 黑馬 程序員B站 Redis課程系列

在點(diǎn)評(píng)項(xiàng)目中,有這樣的需求,如何實(shí)現(xiàn)筆記的好友關(guān)注、以及發(fā)布筆記后推送消息功能?

使用Redis 的 好友關(guān)注、以及發(fā)布筆記后推送消息功能

一、Redis 實(shí)現(xiàn)好友關(guān)注 – 關(guān)注與取消關(guān)注

需求:針對(duì)用戶的操作,可以對(duì)用戶進(jìn)行關(guān)注和取消關(guān)注功能。

在探店圖文的詳情頁(yè)面中,可以關(guān)注發(fā)布筆記的作者

具體實(shí)現(xiàn)思路:基于該表數(shù)據(jù)結(jié)構(gòu),實(shí)現(xiàn)2個(gè)接口

  • 關(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為自增長(zhǎng),簡(jiǎn)化開(kāi)發(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);
}

代碼編寫(xiě)完畢,進(jìn)行測(cè)試

代碼測(cè)試

點(diǎn)擊進(jìn)行關(guān)注用戶

關(guān)注成功

取消關(guān)注

測(cè)試成功

二、Redis 實(shí)現(xiàn)好友關(guān)注 – 共同關(guān)注功能

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

核心代碼如下

UserController

// UserController 根據(jù)id查詢用戶
@GetMapping("/{id}")
public Result queryUserById(@PathVariable("id") Long userId){
    // 查詢?cè)斍?
    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)前頁(yè)數(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)注功能。 在博主個(gè)人頁(yè)面展示出當(dāng)前用戶與博主的共同關(guān)注

思路分析: 使用Redis的Set集合實(shí)現(xiàn),我們把兩人關(guān)注的人分別放入到一個(gè)Set集合中,然后再通過(guò)API去查看兩個(gè)Set集合中的交集數(shù)據(jù)

改造核心代碼

當(dāng)用戶關(guān)注某位用戶后,需要將數(shù)據(jù)存入Redis集合中,方便后續(xù)進(jìn)行共同關(guān)注的實(shí)現(xiàn),同時(shí)取消關(guān)注時(shí),需要?jiǎng)h除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()) {
        // 無(wú)交集
        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)行測(cè)試

?小結(jié)

以上就是【Bug 終結(jié)者】對(duì) 微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn) 好友關(guān)注 的簡(jiǎn)單介紹,Redis 實(shí)現(xiàn)好友關(guān)注功能也是 利用Set集合、ZSet集合實(shí)現(xiàn)這樣一個(gè)需求,同時(shí),采用Redis來(lái)實(shí)現(xiàn)更加的快速,減少系統(tǒng)的消耗,更加快速的實(shí)現(xiàn)數(shù)據(jù)展示! 下篇博文我們繼續(xù) 關(guān)注 如何 使用Redis 實(shí)現(xiàn)推送消息到粉絲收件箱以及滾動(dòng)分頁(yè)查詢!

到此這篇關(guān)于微服務(wù)Spring Boot 整合 Redis 實(shí)現(xiàn) 好友關(guān)注的文章就介紹到這了,更多相關(guān)Spring Boot 整合 Redis 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Windows環(huán)境下打開(kāi)Redis閃退的解決方案

    Windows環(huán)境下打開(kāi)Redis閃退的解決方案

    每次使用完Redis后,我們習(xí)慣性的動(dòng)作是直接叉掉doc頁(yè)面,這樣導(dǎo)致的結(jié)果是Redis在后臺(tái)繼續(xù)運(yùn)行,沒(méi)有關(guān)閉,所以當(dāng)再次打開(kāi)的時(shí)候直接閃退,文中有詳細(xì)的解決方案,需要的朋友可以參考下
    2024-03-03
  • Redis遠(yuǎn)程連接Redis客戶端的實(shí)現(xiàn)步驟

    Redis遠(yuǎn)程連接Redis客戶端的實(shí)現(xiàn)步驟

    本文主要介紹了Redis遠(yuǎn)程連接Redis客戶端的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • redis啟動(dòng),停止,及端口占用處理方法

    redis啟動(dòng),停止,及端口占用處理方法

    今天小編就為大家分享一篇redis啟動(dòng),停止,及端口占用處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 一篇文章帶你弄清楚Redis的精髓

    一篇文章帶你弄清楚Redis的精髓

    Redis是一個(gè)開(kāi)源的、支持網(wǎng)絡(luò)、基于內(nèi)存的鍵值對(duì)存儲(chǔ)系統(tǒng),它可以用作數(shù)據(jù)庫(kù)、緩存和消息中間件。它支持多種數(shù)據(jù)類型,包括字符串、散列、列表、集合、位圖等,擁有極快的讀寫(xiě)速度,并且支持豐富的特性,如事務(wù)、持久化、復(fù)制、腳本、發(fā)布/訂閱等。
    2023-02-02
  • Redis分布式非公平鎖的使用

    Redis分布式非公平鎖的使用

    分布式鎖很多人都能接觸到,本文主要介紹了Redis分布式非公平鎖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringBoot 開(kāi)啟Redis緩存及使用方法

    SpringBoot 開(kāi)啟Redis緩存及使用方法

    用redis做緩存,是因?yàn)閞edis有著很優(yōu)秀的讀寫(xiě)能力,在集群下可以保證數(shù)據(jù)的高可用,那么今天通過(guò)本文給大家講解下SpringBoot使用Redis的緩存的方法,感興趣的朋友一起看看吧
    2021-08-08
  • redis的hGetAll函數(shù)的性能問(wèn)題(記Redis那坑人的HGETALL)

    redis的hGetAll函數(shù)的性能問(wèn)題(記Redis那坑人的HGETALL)

    這篇文章主要介紹了redis的hGetAll函數(shù)的性能問(wèn)題,需要的朋友可以參考下
    2016-02-02
  • 詳解Centos7下配置Redis并開(kāi)機(jī)自啟動(dòng)

    詳解Centos7下配置Redis并開(kāi)機(jī)自啟動(dòng)

    本篇文章主要介紹了Centos7下配置Redis并開(kāi)機(jī)自啟動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • 深入理解redis分布式鎖和消息隊(duì)列

    深入理解redis分布式鎖和消息隊(duì)列

    本篇文章主要介紹了深入理解redis分布式鎖和消息隊(duì)列,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Redis中Scan命令的基本使用教程

    Redis中Scan命令的基本使用教程

    這篇文章主要給大家介紹了關(guān)于Redis中Scan命令的基本使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06

最新評(píng)論