redis实现互相关注功能
在实现社交网络功能中,实现互相关注是必不可少的。在这里,我们将使用redis来实现这个功能,前端使用vue框架实现。
功能要求
我们需要实现以下几个功能:
- 用户能够关注其他用户
- 用户能够取消关注其他用户
- 用户能够查看自己关注的人和被谁关注
- 在用户的主页上,能够显示关注和被关注的数量
redis存储结构设计
我们使用redis的set
数据结构来存储用户关注的人和被关注的人。
具体来说,每个用户都有一个following
和followers
属性,分别表示该用户关注的人和被谁关注。
然后在redis中使用set
类型来存储这些关注信息,在set中,我们将每个关注对象的id存储下来,方便后续的查询。
后端实现
添加关注
我们需要通过api来让用户实现添加关注和取消关注。
下面是添加关注的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中删除。
查看关注对象
最后,我们需要实现查看关注对象的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'); } });
前端实现
在前端中,我们使用vue框架来实现。需要提供以下功能:
- 用户可以通过点击按钮来添加和取消关注操作
- 用户的主页可以显示关注和被关注的数量
添加关注和取消关注操作
在vue中,我们可以使用@click
监听用户点击事件,并在方法中发送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}`); // 更新关注状态 this.isfollowingusers[id] = true; }, // 取消关注 async unfollowuser(id) { await axios.delete(`/api/followers/${id}`); // 更新关注状态 this.isfollowingusers[id] = false; }, // 判断是否关注 isfollowing(id) { return this.isfollowingusers[id]; } }
在这个代码中,我们使用了isfollowingusers
对象来存储所有用户的关注状态。
显示关注和被关注数量
为了在用户的主页上显示关注和被关注的数量,我们需要在后端添加相应的api,并在前端调用数据显示。
下面是相关代码:
// 获取关注和粉丝数量 router.get('/followers/count', async (req, res) => { try { const userid = req.user.id; // 获取关注和被关注数量 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'); } });
<!-- 显示关注和被关注数量 --> <div> <p>关注 {{followingcount}}</p> <p>被关注 {{followercount}}</p> </div>
在这个代码中,我们使用了redis.scard
方法来获取set的数量。
总结
以上就是使用redis实现互相关注功能的全部内容。通过使用redis的set
数据结构来存储关注对象,方便高效地进行添加和取消关注操作。同时,在前端中使用vue框架实现了可交互的关注和取消关注按钮,并在用户主页上显示了关注和被关注的数量。
这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论