简体   繁体   English

没有重复的“喜欢” function 的有效方法?

[英]Efficient way making “Like” function without duplication?

I am making a "Like" function (facebook or instagram kind of), but not sure what is the right way to do it.我正在“点赞”function(facebook 或 instagram 之类的),但不确定什么是正确的做法。

I could think of 2 ways.... (User cannot like same article twice)我可以想到两种方法....(用户不能两次喜欢同一篇文章)

A. "User" data has an array of "Article" IDs... A. “用户”数据有一组“文章”ID...

 // simplified user schema MongoDB const UserSchema ={ id:ObjectID, username:String, likes:[{type:ObjectID,ref:"Article"}] } // simplified article schema const AriticleSchema = { id:ObjectID, title:String, content:String, likes:Number, }

B. "Article" data has an array of "User" IDs... B. “文章”数据有一组“用户”ID...

 // simplified user schema MongoDB const UserSchema ={ id:ObjectID, username:String, } // simplified article schema const AriticleSchema = { id:ObjectID, title:String, content:String, likes:[{type:ObjectID,ref:"User"}], }

I tried both ways and they all worked fine when I only have few users and few articles.我尝试了两种方法,当我只有很少的用户和很少的文章时,它们都运行良好。

but What if I have thousands of "User"s and thousands of "Article"s?但是如果我有数千个“用户”和数千个“文章”呢? I am worrying that everytime I request "User" data or "Article" data(let's say several at a time), I also have to bring arrays of thousands?我担心每次我请求“用户”数据或“文章”数据(一次说几个)时,我还必须带上数千个 arrays 吗? I think there must be better way to do this...我认为必须有更好的方法来做到这一点......

Do you know how people or companies do this?你知道人们或公司是如何做到这一点的吗? I want to know the concept of how "Like" function works.我想知道“喜欢”function 是如何工作的概念。

Thank you.谢谢你。

** Adding some details ** ** 添加一些细节 **

I want "User" can login for reading articles, and press "like button" to like it.我希望“用户”可以登录阅读文章,然后按“喜欢按钮”来喜欢它。 Article "like" will go up by 1 every time unique user likes it (no duplicate).文章“喜欢”将 go 增加 1 每次唯一用户喜欢它(不重复)。 Somebody who already liked the " article" they can "unlike" it or "user" will see they already liked the "article", which means we gotta know that "user" like this "article" or not.已经喜欢“文章”的人可以“不喜欢”它或“用户”会看到他们已经喜欢了“文章”,这意味着我们必须知道“用户”是否喜欢这个“文章”。 Other people dosen't need to know.其他人不需要知道。

I'll extrapolate on a solution by going through the requirements you elaborated step by step.我将通过逐步完成您详细阐述的要求来推断解决方案。

User can login用户可以登录

This will require some system for authorization.这将需要一些系统进行授权。 You could perhaps use another Mongo table dedicated to this sort of thing - at the minimum it should link some authorization token to a user id.您也许可以使用另一个专门用于此类事情的 Mongo 表 - 至少它应该将一些授权令牌链接到用户 ID。

const AuthSchema = {
  user_id: ObjectID,
  auth_token: string,
}

The way you get this auth token is through various means - really depends on how you auth your users, eg phone auth or username/password.您获得此身份验证令牌的方式是通过各种方式 - 实际上取决于您如何对您的用户进行身份验证,例如电话身份验证或用户名/密码。 All that's a bit beyond the scope of this answer.所有这些都超出了这个答案的 scope 。

Article "like" will go up by 1 every time unique user likes it (no duplicate).文章“喜欢”将 go 增加 1 每次唯一用户喜欢它(不重复)。 Somebody who already liked the " article" can "unlike" it or "user" will see they already liked the "article"已经喜欢“文章”的人可以“不喜欢”它,或者“用户”会看到他们已经喜欢了“文章”

A Redis Set would model this quite well.一个 Redis设置将 model 这很好。 Every time a user likes an article, add their user_id into a Redis Set for that article.每次用户喜欢一篇文章时,将他们的 user_id 添加到该文章的 Redis 集中。 The key for such set could look like this:此类集合的密钥可能如下所示:

article:${article_id}:likes

Get the number of people who liked the article by taking the size of its set (full of unique user_ids).通过获取文章集的大小(充满唯一的 user_id)来获取喜欢文章的人数。 Whenever someone unlikes an article, remove their user_id from the set.每当有人不喜欢一篇文章时,从集合中删除他们的 user_id。

Further reading:进一步阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以使此功能在标准浏览器中更有效? - Is there any way of making this function more efficient in a standard browser? 有没有一种方法可以在不重复的情况下将值(如枚举)从前端(JS)连接到后端(PHP/Ruby)? - Is there a way to connect values (like enums) from frontend (JS) to backend (PHP/Ruby) without duplication? 使条件函数更有效 - Making a conditional function more efficient 为不同语言和平台制作库的有效方法 - efficient way of making a library for different languages and platforms 有没有更有效的方法来编写这个函数? - Is there a more efficient way to write this function? 通过id合并两个数组的有效方法,无需重复 - Effective way to merge two arrays by id, without duplication 使图像成为上传功能而无需输入 - Making an image an upload function without making it an input 如何在每个对象实例中没有函数重复的情况下将数据封装在javascript中? - How to encapsulate data in javascript without function duplication in every object instance? 有没有办法更快地更新 function - Is there a way of making a faster updating function 在没有 if 语句的情况下,是否有更有效的方法来执行此代码? - Is there a more efficient way of doing this code without if statements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM