亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Redis實現(xiàn)用戶關注的項目實踐

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

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

功能要求

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

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

Redis存儲結構設計

我們使用Redis的set數(shù)據結構來存儲用戶關注的人和被關注的人。具體來說,每個用戶都有一個followingfollowers屬性,分別表示該用戶關注的人和被誰關注。然后在Redis中使用set類型來存儲這些關注信息,在set中,我們將每個關注對象的id存儲下來,方便后續(xù)的查詢。

后端實現(xiàn)

添加關注

我們需要通過API來讓用戶實現(xiàn)添加關注和取消關注。下面是添加關注的API代碼:

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

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

    // 添加關注對象
    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方法將關注對象的id添加到set中。

取消關注

接下來是取消關注的API代碼:

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

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

    // 刪除關注對象
    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');
  }
});

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

查看關注對象

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

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

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

    // 將id轉換為用戶對象
    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)。需要提供以下功能:

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

添加關注和取消關注操作

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

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

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

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

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

為了在用戶的主頁上顯示關注和被關注的數(shù)量,我們需要在后端添加相應的API,并在前端調用數(shù)據顯示。下面是相關代碼:

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

    // 獲取關注和被關注數(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');
  }
});
<!-- 顯示關注和被關注數(shù)量 -->
<div>
  <p>關注 {{followingCount}}</p>
  <p>被關注 {{followerCount}}</p>
</div>

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

總結

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

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

相關文章

  • Redis RDB與AOF持久化方式詳細講解

    Redis RDB與AOF持久化方式詳細講解

    Redis是基于內存的數(shù)據結構服務器,保存了大量的鍵值對數(shù)據,所以持久化到磁盤是非常必要的,Redis提供了兩種持久化的方式,分別是RDB和AOF。下面我們看下這兩種持久化方式的具體實現(xiàn)原理
    2022-11-11
  • CentOS8.4安裝Redis6.2.6的詳細過程

    CentOS8.4安裝Redis6.2.6的詳細過程

    本文給大家介紹CentOS8.4安裝Redis6.2.6的詳細過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-11-11
  • 關于Redis?bigkeys命令會阻塞問題的解決

    關于Redis?bigkeys命令會阻塞問題的解決

    這篇文章主要介紹了關于Redis?bigkeys命令會阻塞問題的解決,今天分享一次Redis引發(fā)的線上事故,避免再次踩雷,實現(xiàn)快速入門,需要的朋友可以參考下
    2023-03-03
  • Redis緩存高可用集群詳解

    Redis緩存高可用集群詳解

    Redis集群提供了哨兵模式和高可用集群模式兩種方案,前者適合低并發(fā),配置復雜,主從切換可能導致瞬斷;后者通過多主多從結構提高可用性和性能,支持線性擴展,配置簡單,搭建Redis集群至少需要三個主節(jié)點
    2024-10-10
  • 在Mac OS上安裝Vagrant和Docker的教程

    在Mac OS上安裝Vagrant和Docker的教程

    這篇文章主要介紹了在Mac OS上安裝Vagrant和Docker的教程,并安裝和設置Postgres和Elasticsearch和Redis,需要的朋友可以參考下
    2015-04-04
  • Redis migrate數(shù)據遷移工具的使用教程

    Redis migrate數(shù)據遷移工具的使用教程

    這篇文章主要給大家介紹了關于Redis migrate數(shù)據遷移工具的使用教程,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-08-08
  • Redis字符串原理的深入理解

    Redis字符串原理的深入理解

    這篇文章主要給大家介紹了關于Redis字符串原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • Caffeine實現(xiàn)類似redis的動態(tài)過期時間設置示例

    Caffeine實現(xiàn)類似redis的動態(tài)過期時間設置示例

    這篇文章主要為大家介紹了Caffeine實現(xiàn)類似redis的動態(tài)過期時間示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • redis實現(xiàn)簡單分布式鎖

    redis實現(xiàn)簡單分布式鎖

    這篇文章主要介紹了redis實現(xiàn)簡單分布式鎖,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下
    2013-09-09
  • macOS上Redis的安裝與測試操作

    macOS上Redis的安裝與測試操作

    這篇文章主要介紹了macOS上Redis的安裝與測試操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07

最新評論