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

微服務Spring Boot 整合 Redis 實現(xiàn)好友關注功能

 更新時間:2022年12月14日 10:59:30   作者:Bug 終結者  
這篇文章主要介紹了微服務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閃退的解決方案

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

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

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

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

    redis啟動,停止,及端口占用處理方法

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

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

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

    Redis分布式非公平鎖的使用

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

    SpringBoot 開啟Redis緩存及使用方法

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

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

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

    詳解Centos7下配置Redis并開機自啟動

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

    深入理解redis分布式鎖和消息隊列

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

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

    這篇文章主要給大家介紹了關于Redis中Scan命令的基本使用教程,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06

最新評論