使用 php/MySQL 进行定时事件

发布于 2024-07-06 03:44:05 字数 375 浏览 4 评论 0原文

我需要一种方法来在一段时间过去后修改表中的值。 我当前的方法如下:

  • 当用户加载请求更改值的页面时,在表中插入等待期的结束时间
  • ,检查当前>=结束时间,
  • 如果是,则更改值并删除结束时间字段,如果不是,则不执行任何操作

这将是网站的一大特色,因此效率是关键; 考虑到这一点,您可能会发现我的做法存在问题。 每次有人访问需要该信息的页面时,都会调用相同的代码块。

任何有关改进或更好方法的建议将不胜感激,最好是 php 或 perl。

回应 cron job 的答案: 谢谢,如果可能的话我想做类似的事情,但主机限制是问题所在。 由于这是应用程序的主要部分,因此不能对其进行限制。

I need a way to modify a value in a table after a certain amount of time has passed. My current method is as follow:

  • insert end time for wait period in table
  • when a user loads a page requesting the value to be changed, check to see if current >= end time
  • if it is, change the value and remove the end time field, if it isn't, do nothing

This is going to be a major feature on the site, and so efficiency is the key; with that in mind, you can probably see the problem with how I'm doing it. That same chunk of code is going to be called every time someone access a page that needs the information.

Any suggestions for improvements or better methods would be greatly appreciated, preferably in php or perl.

In response to cron job answers:
Thanks, and I'd like to do something like that if possible, but hosts limits are the problem. Since this is a major part of the app, it can't be limited.

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

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

发布评论

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

评论(5

绳情 2024-07-13 03:44:05

为什么不使用 cron 在幕后更新这些信息呢? 这样您就可以减轻对每个页面点击的检查,并且可以实际安排时间以满足您的应用程序的要求。

why not use a cron to update this information behind the scenes? that way you offload the checks on each page hit, and can actually schedule the timing to meet your app's requirements.

靖瑶 2024-07-13 03:44:05

您的解决方案听起来非常合乎逻辑,因为您无权访问 cron。 另一种方法可以是将值存储在文件中,下次加载页面时检查上次修改的时间(filemtime(“checkfile.txt”)),并决定是否需要再次修改。 您应该测试这两种方法的性能。

Your solution sounds very logical, since you don't have access to cron. Another way could be storing the value in a file, and the next time the page is loaded check when it was last modified (filemtime("checkfile.txt")), and decide if it needs modifying again. You should test performance for both methods.

云雾 2024-07-13 03:44:05

您可以使用 cron 作业定期检查数据库中的每个字段并以这种方式更新吗?

其中很大一部分是需要更新的频率。 许多共享主机限制 cron 检查的频率,例如不超过每 15 分钟一次,这可能会影响应用程序。

Can you use a cron job to check each field in the database periodically and update that way?

A big part of this is how frequently the updates are required. A lot of shared hosts limit the frequency of cron checks, for example no more than every 15 minutes, which could affect the application.

一场春暖 2024-07-13 03:44:05

您可以在每次页面加载时使用某种触发器。 我真的不知道这会如何影响性能,但也许其他人可以提供一些线索。

You could use a trigger of some sort on each page load. I really have no idea how that would affect performance but maybe somebody else can shed some light.

也只是曾经 2024-07-13 03:44:05

如果性能确实开始成为一个问题(这意味着比您可能意识到的要多得多),您可以使用 memchached 来存储信息......

If performance really starts to be an issue, (which means a lot more than you probably realize) you could use memchached to store the info...

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