如何从用户关注的人创建文件的提要? [英] How to create a feed of files from people that a user is following?

查看:69
本文介绍了如何从用户关注的人创建文件的提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP和MongoDB创建API.在这个系统中,我有用户,每个用户都可以上传文件.用户还可以互相关注".

I am creating an API using PHP and MongoDB. In this system I got users and each user can upload files. Users can also "follow" each other.

我需要返回已认证用户正在关注的用户上传的所有最新文件的提要.我不太确定该如何设计和执行.

I need to return a feed of all the latest files uploaded by users that the authenticated user is following. I am not really sure how to design and execute this.

这就是我要实现的目标.

This is what I am thinking of implementing.

  1. 获取该用户关注的所有用户.
  2. 浏览此用户数组,并为每个用户获取最新的4个文件,并将它们添加到数组中.
  3. 以某种方式按创建日期对这些文件(此数组)进行排序.
  4. 退货.

这真的是最佳方法吗?有没有更好的办法?用户被保存在集合用户中,文件被保存在集合文件中.以下内容保存在集合users.followers中.

Is this really the optimal way? Is there a better way? Users are saved in the collection Users and files in the collection files. Following is saved in the collection users.followers.

推荐答案

这是扇入与扇出的问题.我建议您尝试散开:

This is the fan-in vs. fan-out problem. I'd suggest you try fan-out:

为您的用户保留一个feed集合.用户上载文档时,请在她的每个好友供稿项集合中插入一个新的供稿项.该集合可能如下所示:

Keep a feed collection for your users. When a user uploads a document, insert a new feed item in each of her friends feed item collection. The collection could look like this:

{
    "_id": (some id)
    "UserId": (id of the user who 'owns', i.e. reads this feed)
    "FriendId": (if of the friend who posted the file)
    "FriendName": "John Doe" (name of the fried, denormalized)
    "Timestamp": ...
}

使用复合索引{UserId, Timestamp}.

这种方法需要大量写入:如果Jane拥有数百个朋友,那么这数百个插入将花费他们的时间.另一方面,上载文件通常要花费很多时间,因此开销可以忽略不计,并且读取操作非常简单.

This approach is write-heavy: If Jane has hundreds of friends, these hundreds of inserts will take their time. On the other hand, uploading a file generally takes a lot of time anyway, so the overhead is negligible, and your reads will be ridiculously simple.

当然,可以更费力地进一步优化,但是应该可以应付大量流量.

Of course, this can be further optimized with more effort, but it should do fine for quite a bit of traffic.

这篇关于如何从用户关注的人创建文件的提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆