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

Redis之如何實現(xiàn)用戶關(guān)注

 更新時間:2025年03月20日 16:53:29   作者:沙漠真有魚  
這篇文章主要介紹了Redis之如何實現(xiàn)用戶關(guān)注問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Redis實現(xiàn)互相關(guān)注功能

在實現(xiàn)社交網(wǎng)絡功能中,實現(xiàn)互相關(guān)注是必不可少的。在這里,我們將使用Redis來實現(xiàn)這個功能,前端使用Vue框架實現(xiàn)。

功能要求

我們需要實現(xiàn)以下幾個功能:

  1. 用戶能夠關(guān)注其他用戶
  2. 用戶能夠取消關(guān)注其他用戶
  3. 用戶能夠查看自己關(guān)注的人和被誰關(guān)注
  4. 在用戶的主頁上,能夠顯示關(guān)注和被關(guān)注的數(shù)量

Redis存儲結(jié)構(gòu)設計

我們使用Redis的set數(shù)據(jù)結(jié)構(gòu)來存儲用戶關(guān)注的人和被關(guān)注的人。

具體來說,每個用戶都有一個followingfollowers屬性,分別表示該用戶關(guān)注的人和被誰關(guān)注。

然后在Redis中使用set類型來存儲這些關(guān)注信息,在set中,我們將每個關(guān)注對象的id存儲下來,方便后續(xù)的查詢。

后端實現(xiàn)

添加關(guān)注

我們需要通過API來讓用戶實現(xiàn)添加關(guān)注和取消關(guān)注。

下面是添加關(guān)注的API代碼:

// 添加關(guān)注
router.post('/followers/:id', async (req, res) => {
  try {
    const followerId = req.user.id;
    const followingId = req.params.id;

    // 獲取被關(guān)注的用戶和關(guān)注該用戶的用戶
    const following = await User.findById(followingId);
    const follower = await User.findById(followerId);

    // 添加關(guān)注對象
    await redis.sadd(`user:${followerId}:following`, followingId);
    await redis.sadd(`user:${followingId}:followers`, followerId);

    res.json({ message: `You are now following ${following.username}` });
  } catch (error) {
    console.error(error.message);
    res.status(500).send('Server Error');
  }
});

在這個代碼中,我們使用了redis.sadd方法將關(guān)注對象的id添加到set中。

取消關(guān)注

接下來是取消關(guān)注的API代碼:

// 取消關(guān)注
router.delete('/followers/:id', async (req, res) => {
 try {
    const followerId = req.user.id;
    const followingId = req.params.id;

    // 獲取被取消關(guān)注的用戶和取消關(guān)注該用戶的用戶
    const following = await User.findById(followingId);
    const follower = await User.findById(followerId);

    // 刪除關(guān)注對象
    await redis.srem(`user:${followerId}:following`, followingId);
    await redis.srem(`user:${followingId}:followers`, followerId);

    res.json({ message: `You have unfollowed ${following.username}` });
  } catch (error) {
    console.error(error.message);
    res.status(500).send('Server Error');
  }
});

這個代碼與添加關(guān)注的代碼類似,只是使用了redis.srem方法來將關(guān)注對象的id從set中刪除。

查看關(guān)注對象

最后,我們需要實現(xiàn)查看關(guān)注對象的API。這個API需要分別獲取關(guān)注和被關(guān)注的set,然后將id轉(zhuǎn)換為用戶對象。

// 獲取關(guān)注和粉絲
router.get('/followers', async (req, res) => {
  try {
    const userId = req.user.id;

    // 獲取關(guān)注和被關(guān)注的set
    const [following, followers] = await Promise.all([
      redis.smembers(`user:${userId}:following`),
      redis.smembers(`user:${userId}:followers`),
    ]);

    // 將id轉(zhuǎn)換為用戶對象
    const followingUsers = await Promise.all(
      following.map((id) => User.findById(id))
    );
    const followerUsers = await Promise.all(
      followers.map((id) => User.findById(id))
    );

    res.json({ following: followingUsers, followers: followerUsers });
  } catch (error) {
    console.error(error.message);
    res.status(500).send('Server Error');
  }
});

前端實現(xiàn)

在前端中,我們使用Vue框架來實現(xiàn)。需要提供以下功能:

  1. 用戶可以通過點擊按鈕來添加和取消關(guān)注操作
  2. 用戶的主頁可以顯示關(guān)注和被關(guān)注的數(shù)量

添加關(guān)注和取消關(guān)注操作

在Vue中,我們可以使用@click監(jiān)聽用戶點擊事件,并在方法中發(fā)送API請求來進行添加和取消關(guān)注。

下面是代碼示例:

<!-- 添加關(guān)注 -->
<button @click="followUser(user._id)" v-if="!isFollowing(user._id)">關(guān)注</button>

<!-- 取消關(guān)注 -->
<button @click="unfollowUser(user._id)" v-else>取消關(guān)注</button>
methods: {
  // 添加關(guān)注
  async followUser(id) {
    await axios.post(`/api/followers/${id}`);
    // 更新關(guān)注狀態(tài)
    this.isFollowingUsers[id] = true;
  },
  // 取消關(guān)注
  async unfollowUser(id) {
    await axios.delete(`/api/followers/${id}`);
    // 更新關(guān)注狀態(tài)
    this.isFollowingUsers[id] = false;
  },
  // 判斷是否關(guān)注
  isFollowing(id) {
    return this.isFollowingUsers[id];
  }
}

在這個代碼中,我們使用了isFollowingUsers對象來存儲所有用戶的關(guān)注狀態(tài)。

顯示關(guān)注和被關(guān)注數(shù)量

為了在用戶的主頁上顯示關(guān)注和被關(guān)注的數(shù)量,我們需要在后端添加相應的API,并在前端調(diào)用數(shù)據(jù)顯示。

下面是相關(guān)代碼:

// 獲取關(guān)注和粉絲數(shù)量
router.get('/followers/count', async (req, res) => {
  try {
    const userId = req.user.id;

    // 獲取關(guān)注和被關(guān)注數(shù)量
    const [followingCount, followerCount] = await Promise.all([
      redis.scard(`user:${userId}:following`),
      redis.scard(`user:${userId}:followers`),
    ]);

    res.json({ followingCount, followerCount });
  } catch (error) {
    console.error(error.message);
    res.status(500).send('Server Error');
  }
});
<!-- 顯示關(guān)注和被關(guān)注數(shù)量 -->
<div>
  <p>關(guān)注 {{followingCount}}</p>
  <p>被關(guān)注 {{followerCount}}</p>
</div>

在這個代碼中,我們使用了redis.scard方法來獲取set的數(shù)量。

總結(jié)

以上就是使用Redis實現(xiàn)互相關(guān)注功能的全部內(nèi)容。通過使用Redis的set數(shù)據(jù)結(jié)構(gòu)來存儲關(guān)注對象,方便高效地進行添加和取消關(guān)注操作。同時,在前端中使用Vue框架實現(xiàn)了可交互的關(guān)注和取消關(guān)注按鈕,并在用戶主頁上顯示了關(guān)注和被關(guān)注的數(shù)量。

這些僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 你了解Redis事務嗎

    你了解Redis事務嗎

    說到事務,大家會立刻想到Mysql的事務,所謂的事務就是對數(shù)據(jù)進行一系列的操作,要么都執(zhí)行成功,要么都執(zhí)行失敗,下面就介紹一下Redis如何實現(xiàn)事務,感興趣的可以了解一下
    2022-08-08
  • 詳解Redis中的List是如何實現(xiàn)的

    詳解Redis中的List是如何實現(xiàn)的

    List 的 Redis 中的 5 種主要數(shù)據(jù)結(jié)構(gòu)之一,它是一種序列集合,可以存儲一個有序的字符串列表,順序是插入的順序,本文將給大家介紹了一下Redis中的List是如何實現(xiàn)的,需要的朋友可以參考下
    2024-05-05
  • Redis Cluster的圖文講解

    Redis Cluster的圖文講解

    今天小編就為大家分享一篇關(guān)于Redis Cluster的圖文講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • window下創(chuàng)建redis出現(xiàn)問題小結(jié)

    window下創(chuàng)建redis出現(xiàn)問題小結(jié)

    這篇文章主要介紹了window下創(chuàng)建redis出現(xiàn)問題總結(jié),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • redis實現(xiàn)簡單隊列

    redis實現(xiàn)簡單隊列

    這篇文章主要為大家詳細介紹了redis實現(xiàn)簡單隊列的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Redis之常用數(shù)據(jù)結(jié)構(gòu)哈希表

    Redis之常用數(shù)據(jù)結(jié)構(gòu)哈希表

    這篇文章主要介紹了Redis常用的數(shù)據(jù)結(jié)構(gòu)哈希表,哈希表是一種保存鍵值對的數(shù)據(jù)結(jié)構(gòu),具有一定的參考價值,需要的朋友可以參考閱讀
    2023-04-04
  • Redis中有序集合的內(nèi)部實現(xiàn)方式的詳細介紹

    Redis中有序集合的內(nèi)部實現(xiàn)方式的詳細介紹

    本文主要介紹了Redis中有序集合的內(nèi)部實現(xiàn)方式的詳細介紹,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Redis序列化反序列化不一致導致String類型值多了雙引號問題

    Redis序列化反序列化不一致導致String類型值多了雙引號問題

    這篇文章主要介紹了Redis序列化反序列化不一致導致String類型值多了雙引號問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 關(guān)于Redis數(shù)據(jù)持久化的概念介紹

    關(guān)于Redis數(shù)據(jù)持久化的概念介紹

    Redis是內(nèi)存數(shù)據(jù)庫,數(shù)據(jù)都是存儲在內(nèi)存中,需要定期將Redis中的數(shù)據(jù)以某種形式(或命數(shù)據(jù)令)從內(nèi)存保存到硬盤,今天給大家分享Redis數(shù)據(jù)的持久化的概念介紹,需要的朋友參考下吧
    2021-08-08
  • Linux服務器安裝redis數(shù)據(jù)庫圖文教程

    Linux服務器安裝redis數(shù)據(jù)庫圖文教程

    Redis是一個開源的使用ANSI C語言編寫、遵守BSD協(xié)議、支持網(wǎng)絡、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。這篇文章主要介紹了Linux服務器安裝redis數(shù)據(jù)庫圖文教程,需要的朋友可以參考下
    2018-03-03

最新評論