最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微服務(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)化開發(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)行測(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集合中,然后再通過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)文章

  • 一文弄懂Redis單線程和多線程

    一文弄懂Redis單線程和多線程

    本文主要介紹了一文弄懂Redis單線程和多線程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 如何保證Redis與數(shù)據(jù)庫(kù)的數(shù)據(jù)一致性

    如何保證Redis與數(shù)據(jù)庫(kù)的數(shù)據(jù)一致性

    這篇文章主要介紹了如何保證Redis與數(shù)據(jù)庫(kù)的數(shù)據(jù)一致性,文中舉了兩個(gè)場(chǎng)景例子介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Redis應(yīng)用之簽到的使用

    Redis應(yīng)用之簽到的使用

    在很多時(shí)候,我們遇到用戶簽到的場(chǎng)景,本文主要介紹了Redis應(yīng)用之簽到的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • 無(wú)法啟動(dòng)Redis打開redis-server閃退的問題解決辦法

    無(wú)法啟動(dòng)Redis打開redis-server閃退的問題解決辦法

    正常開啟redis服務(wù),首先要啟動(dòng)redis-server.exe,但是閃退,導(dǎo)致無(wú)法開啟redis服務(wù),這篇文章主要給大家介紹了關(guān)于無(wú)法啟動(dòng)Redis打開redis-server閃退問題的解決辦法,需要的朋友可以參考下
    2024-07-07
  • redis搭建哨兵模式實(shí)現(xiàn)一主兩從三哨兵

    redis搭建哨兵模式實(shí)現(xiàn)一主兩從三哨兵

    本文主要介紹了redis搭建哨兵模式實(shí)現(xiàn)一主兩從三哨兵,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • 如何高效地向Redis插入大量的數(shù)據(jù)(推薦)

    如何高效地向Redis插入大量的數(shù)據(jù)(推薦)

    本篇文章主要介紹了如何高效地向Redis插入大量的數(shù)據(jù),現(xiàn)在分享給大家,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • gem install redis報(bào)錯(cuò)的解決方案

    gem install redis報(bào)錯(cuò)的解決方案

    今天小編就為大家分享一篇關(guān)于gem install redis報(bào)錯(cuò)的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 淺談Redis高并發(fā)緩存架構(gòu)性能優(yōu)化實(shí)戰(zhàn)

    淺談Redis高并發(fā)緩存架構(gòu)性能優(yōu)化實(shí)戰(zhàn)

    本文主要介紹了淺談Redis高并發(fā)緩存架構(gòu)性能優(yōu)化實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Redis如何批量刪除指定模糊的key舉例

    Redis如何批量刪除指定模糊的key舉例

    在實(shí)際項(xiàng)目中,我們可能需要根據(jù)一定的條件來(lái)刪除部分key,這時(shí)候就需要用到模糊刪除操作這,下面篇文章主要給大家介紹了關(guān)于Redis如何批量刪除指定模糊的key的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • Redis分片集群的實(shí)現(xiàn)

    Redis分片集群的實(shí)現(xiàn)

    Redis 分片集群是一種將 Redis數(shù)據(jù)庫(kù)分散到多個(gè)節(jié)點(diǎn)上的方式,以提供更高的性能和可伸縮性,本文主要介紹了Redis分片集群的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-04-04

最新評(píng)論

独山县| 盐山县| 会宁县| 宝鸡市| 宁化县| 喀喇| 楚雄市| 丹巴县| 临沧市| 永修县| 罗定市| 顺义区| 宝应县| 古浪县| 那曲县| 沐川县| 旬阳县| 三都| 新兴县| 哈尔滨市| 万盛区| 扬中市| 河曲县| 筠连县| 辽中县| 涞源县| 南宫市| 扎兰屯市| 德阳市| 临安市| 丹凤县| 二连浩特市| 阿勒泰市| 印江| 枣阳市| 惠东县| 临沭县| 萨迦县| 四会市| 南宁市| 阿勒泰市|