php 遵循机制:使用什么策略?

发布于 2024-09-24 11:37:01 字数 488 浏览 6 评论 0原文

我正在尝试建立一个类似推特的关注机制。用户采取行动。我们列出所有用户的关注者的列表,然后用一些信息填充他们的所有流。由于这可能需要一些时间(如果您有 10,000 个关注者,则需要 10,000 个流来插入信息,即可能需要 10,000 个 SQL 调用),所以我想确保这是在后台完成的,而采取操作的用户可以继续他的生活。

所以,我正在考虑的策略是:

  • 用户采取行动。
  • php 脚本将打开另一个 php 脚本,该脚本将完成所有工作,并且可能需要一两秒钟。
  • 同时,采取行动的用户可以继续他们的生活,他们的脚本会继续,而且速度很快。

想法?我也尝试过使用队列,比如 SQS,但这种方法听起来似乎也可行?另外,它的优点(对我来说)是更容易在本地测试并且更容易在非 ec2 主机上运行。

如果这是一个好方法,我将如何从 php 脚本中打开 php 脚本?它是否可以像(如果 php 脚本位于某个 url 中)获取该脚本所在的 url 一样简单?

I am trying to build a twitter-like follow mechanism. User takes an action. We make a list of all that users' followers, and then populate all of their streams with some information. Since this can take some time (if you have 10,000 followers that's 10,000 streams to insert information in, ie. 10,000 SQL calls perhaps), I want to make sure that this is done in the background, while the user that takes the action can go on with his life.

So, the strategy I'm considering is this:

  • user takes action.
  • php script opens another php script that will do all the work and that might take a second or two.
  • meanwhile, the user that took the action can go on with their life, their script just goes on and it's fast.

Thoughts? I also played around with using a queue, something like SQS, but this approach sounds like it might also work? Plus it has the advantage (for me) that it's easier to test locally and easier to run on non ec2 hosts.

And if this is a good approach, how would I open a php script from within a php script? Could it be as simple as (if the php script lives at a url) doing a get on a url where that script lives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2024-10-01 11:37:01

描述的方式听起来像是您想要为关注该用户的每个人复制/复制第一个用户的帖子?这将是一场数据存储噩梦。

你应该从另一个角度来看待它。考虑以下模型:

用户 A 发布了他早餐吃的东西。该信息与他的用户 ID 一起存储在表中一次。

用户B查看他的“流”。这是动态创建的帖子列表。此时用户 B 正在关注 50 人。用户 B 的脚本将获取他所关注的人的前 50 个最新帖子,并在他的“流”上为他显示这些帖子

。使用此模型,每个用户每次琐碎的早餐更新永远不会有超过一个帖子。此外,关注者的数量不会增加发布推文所需的处理时间。我的意思是推文。

澄清

您只需要进行一些标准化即可。因此,您将有一个 users 表、一个 users_following 表和一个 posts 表。该查询类似于:

SELECT posts.* FROM users_following
         LEFT JOIN posts ON posts.user_id = users_following.followed
         WHERE users_following.follower = $idOfUserB
         ORDER BY posts.created LIMIT 50;

The way this is being described sounds like you want to replicate/duplicate the first user's post for everyone who follows that user? That's going to be a data storage nightmare.

You should look at it from the other point of view. Consider the following model:

User A posts what he ate for breakfast. This is stored once in a table with his user id.

User B looks at his "stream". This is a dynamically created list of posts. At this point User B is following 50 people. User B's script will get the first 50 most recent posts of whomever he is following and display them for him on his "stream"

With this model, you never have more than one post per user per frivolous breakfast update. Also, the number of followers does not scale up the processing time needed to publish the twit. I mean tweet.

Clarification

You'll just need to do some normalization. So you'll have a users table, a users_following table, and a posts table. The query would look akin to:

SELECT posts.* FROM users_following
         LEFT JOIN posts ON posts.user_id = users_following.followed
         WHERE users_following.follower = $idOfUserB
         ORDER BY posts.created LIMIT 50;
烟沫凡尘 2024-10-01 11:37:01

如果您希望您的网站能够扩展。

  • 首先,您需要使用消息队列,例如 redis/beanstalkd/gearmand。
  • 其次,您需要使用例如 redis/memcached 在内存中执行操作。确保您有足够的内存来将活动数据集保存在内存中。

(如果您有 10,000 名关注者,那就是
10,000 个流插入信息
在,即可能有 10,000 个 SQL 调用)

10,000 个 SQL 调用已经被失败鲸鱼覆盖了。对于这样的应用程序,我不会使用 MySQL(或者至少将其与 memcached 一起使用),而是使用 redis。将活动数据集保留在内存中。保持数据模型尽可能简单。

如果这是一个好方法,那么如何
我可以从内部打开一个 php 脚本吗
PHP 脚本?

不要那样做。通过lpush将消息添加到redis的阻止列表并通过blpop(守护进程)读取它们。我将首先填充在线用户列表,然后填充离线用户列表。离线用户不介意延迟,因为他们不在线。您可以将对密钥的引用放在关注该人的所有用户的列表中,并通过 mget 获取所有密钥。

是否可以像(如果 php
脚本位于 url)执行 get on
该脚本所在的 url?

再次强调不要调用 url,而是使用消息队列。调用 url 会给您带来不必要的开销。

太棒了。回到 SQL :) 这将是
即使您关注 500 也很快
人们? –

SQL 会在高负载下给你失败的鲸鱼很大的时间。至少你需要memcached!但我会使用redis来代替。

If you want your site to scale at all.

  • First you need to use a message queue like for example redis/beanstalkd/gearmand.
  • Second you need to do your operations in memory using for example redis/memcached. Make sure you have enough memory to keep active dataset in memory.

(if you have 10,000 followers that's
10,000 streams to insert information
in, ie. 10,000 SQL calls perhaps)

10,000 SQL calls has fail whale written over it. I would not use MySQL(Or at least use it with memcached) for such an application but use redis. Keep active dataset in memory. Keep datamodel as simple as possible.

And if this is a good approach, how
would I open a php script from within
a php script?

Don't do that. Add messages to redis's blocking list via lpush and read them via blpop(daemon process). I would first populate list of online users and next populate list of offline users. Offline users don't mind delay because they aren't online. You would put reference to key on list of all users following that person and get all the keys via mget.

Could it be as simple as (if the php
script lives at a url) doing a get on
a url where that script lives?

Again don't call urls but use message queue. Calling url will give you undesired overhead.

Awesome. Back to SQL :) This will be
fast even if you're following 500
people? –

SQL will give you fail whales big time on high load. At least you will need memcached! But I would use redis instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文