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

Redis實(shí)現(xiàn)用戶關(guān)注的項(xiàng)目實(shí)踐

 更新時(shí)間:2024年02月28日 14:45:49   作者:Blet-  
本文主要介紹了Redis實(shí)現(xiàn)用戶關(guān)注的項(xiàng)目實(shí)踐,通過(guò)使用Redis的set數(shù)據(jù)結(jié)構(gòu)來(lái)存儲(chǔ)關(guān)注對(duì)象,方便高效地進(jìn)行添加和取消關(guān)注操作,具有一定的參考價(jià)值,感興趣的可以了解一下

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

功能要求

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

  • 用戶能夠關(guān)注其他用戶
  • 用戶能夠取消關(guān)注其他用戶
  • 用戶能夠查看自己關(guān)注的人和被誰(shuí)關(guān)注
  • 在用戶的主頁(yè)上,能夠顯示關(guān)注和被關(guān)注的數(shù)量

Redis存儲(chǔ)結(jié)構(gòu)設(shè)計(jì)

我們使用Redis的set數(shù)據(jù)結(jié)構(gòu)來(lái)存儲(chǔ)用戶關(guān)注的人和被關(guān)注的人。具體來(lái)說(shuō),每個(gè)用戶都有一個(gè)followingfollowers屬性,分別表示該用戶關(guān)注的人和被誰(shuí)關(guān)注。然后在Redis中使用set類型來(lái)存儲(chǔ)這些關(guān)注信息,在set中,我們將每個(gè)關(guān)注對(duì)象的id存儲(chǔ)下來(lái),方便后續(xù)的查詢。

后端實(shí)現(xiàn)

添加關(guān)注

我們需要通過(guò)API來(lái)讓用戶實(shí)現(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)注對(duì)象
    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');
  }
});

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

取消關(guān)注

接下來(lái)是取消關(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)注對(duì)象
    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');
  }
});

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

查看關(guān)注對(duì)象

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

// 獲取關(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)換為用戶對(duì)象
    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');
  }
});

前端實(shí)現(xiàn)

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

  • 用戶可以通過(guò)點(diǎn)擊按鈕來(lái)添加和取消關(guān)注操作
  • 用戶的主頁(yè)可以顯示關(guān)注和被關(guān)注的數(shù)量

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

在Vue中,我們可以使用@click監(jiān)聽(tīng)用戶點(diǎn)擊事件,并在方法中發(fā)送API請(qǐng)求來(lái)進(jìn)行添加和取消關(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];
  }
}

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

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

為了在用戶的主頁(yè)上顯示關(guān)注和被關(guān)注的數(shù)量,我們需要在后端添加相應(yīng)的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>

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

總結(jié)

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

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

相關(guān)文章

  • Redis Cluster原理及配置詳解

    Redis Cluster原理及配置詳解

    這篇文章主要為大家介紹了Redis Cluster原理及配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • redis?Template.opsForValue()中方法實(shí)例詳解

    redis?Template.opsForValue()中方法實(shí)例詳解

    這篇文章主要介紹了redis?Template.opsForValue()中方法講解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • redis GEO數(shù)據(jù)結(jié)構(gòu)、實(shí)現(xiàn)附近商鋪功能實(shí)踐

    redis GEO數(shù)據(jù)結(jié)構(gòu)、實(shí)現(xiàn)附近商鋪功能實(shí)踐

    文章介紹了Redis中的GEO命令及其用途,包括地理坐標(biāo)存儲(chǔ)、距離計(jì)算、坐標(biāo)轉(zhuǎn)換和位置搜索等功能,還分享了如何使用Redis實(shí)現(xiàn)查詢附近商鋪的功能,包括導(dǎo)入商鋪信息和根據(jù)類型及距離進(jìn)行搜索
    2025-12-12
  • Redis List列表的詳細(xì)介紹

    Redis List列表的詳細(xì)介紹

    這篇文章主要介紹了Redis List列表的詳細(xì)介紹的相關(guān)資料,Redis列表是簡(jiǎn)單的字符串列表,按照插入順序排序,需要的朋友可以參考下
    2017-08-08
  • 如何使用?redis?消息隊(duì)列完成秒殺過(guò)期訂單處理操作(二)

    如何使用?redis?消息隊(duì)列完成秒殺過(guò)期訂單處理操作(二)

    這篇文章主要介紹了如何使用?redis?消息隊(duì)列完成秒殺過(guò)期訂單處理操作,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • 關(guān)于Redis單線程的正確理解

    關(guān)于Redis單線程的正確理解

    很多同學(xué)對(duì)Redis的單線程和I/O多路復(fù)用技術(shù)并不是很了解,所以我用簡(jiǎn)單易懂的語(yǔ)言讓大家了解下Redis單線程和I/O多路復(fù)用技術(shù)的原理,對(duì)學(xué)好和運(yùn)用好Redis打下基礎(chǔ),感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • Redis數(shù)據(jù)一致性問(wèn)題的三種解決方案

    Redis數(shù)據(jù)一致性問(wèn)題的三種解決方案

    Redis(Remote?Dictionary?Server?),是一個(gè)高性能的基于Key-Value結(jié)構(gòu)存儲(chǔ)的NoSQL開(kāi)源數(shù)據(jù)庫(kù),大部分公司采用Redis來(lái)實(shí)現(xiàn)分布式緩存,用來(lái)提高數(shù)據(jù)查詢效率,本文就給大家介紹三種Redis數(shù)據(jù)一致性問(wèn)題的解決方案,需要的朋友可以參考下
    2023-07-07
  • 詳解Redis緩存與Mysql如何保證雙寫一致

    詳解Redis緩存與Mysql如何保證雙寫一致

    緩存和數(shù)據(jù)庫(kù)如何保證數(shù)據(jù)的一致是個(gè)很經(jīng)典的問(wèn)題,這篇文章就來(lái)和大家一起探討一下Redis緩存與Mysql如何保證雙寫一致,感興趣的小伙伴可以參考下
    2023-12-12
  • Redis瞬時(shí)高并發(fā)秒殺方案總結(jié)

    Redis瞬時(shí)高并發(fā)秒殺方案總結(jié)

    本文講述了Redis瞬時(shí)高并發(fā)秒殺方案總結(jié),具有很好的參考價(jià)值,感興趣的小伙伴們可以參考一下,具體如下:
    2018-05-05
  • 如何打造redis緩存組件

    如何打造redis緩存組件

    文章介紹了如何使用熱插拔AOP、反射、Redis自定義注解和SpringEL表達(dá)式來(lái)打造一個(gè)優(yōu)雅的Redis緩存組件,通過(guò)這種方式,可以重構(gòu)和簡(jiǎn)化緩存代碼,并提供了Redis配置和自定義注解的詳細(xì)說(shuō)明,文章還包含了AOP測(cè)試的總結(jié),并鼓勵(lì)讀者參考和支持
    2024-12-12

最新評(píng)論

永福县| 广宁县| 岳池县| 喀喇沁旗| 安乡县| 攀枝花市| 邛崃市| 武功县| 榆林市| 博爱县| 沁源县| 平安县| 绥中县| 子洲县| 吉木萨尔县| 克拉玛依市| 阿坝县| 抚顺县| 新干县| 勃利县| 吴旗县| 昌吉市| 监利县| 衢州市| 宜都市| 运城市| 宿州市| 准格尔旗| 南京市| 石泉县| 华池县| 邮箱| 务川| 沂源县| 吉隆县| 旺苍县| 资溪县| 邹城市| 象州县| 邓州市| 北碚区|