每秒自动数据库读/写
我正在制作一个拍卖网站,它有一个自动投标系统。该系统让人们无需到场即可进行投标。
我的问题是如何实施这样一个系统。我已经制作了 php 文件,一切都准备好了,我制作了一个每秒刷新一次的 html 页面。它有效,但我想知道是否有更好的解决方案。
我尝试的第二个选项是制作一个每秒打开网页的java应用程序。这会消耗“大量”内存/CPU。另外,我认为某处存在内存泄漏。
我正在使用 centOS 和 Plesk 的专用 Linux 服务器上运行此应用程序,因此我确信有很多替代方案。
例如:
每分钟执行一个 php 文件的 cronjob,其中 php 文件循环一分钟并休眠。
一个循环 1 秒的 php 文件会休眠。
一个 cronjob,在一秒后执行并调度另一个 cronjob。
MySQL 调度程序,但我不知道如何实现它,我听说它会锁定数据库以进行其他写入。
我也听说过 cli、perl、python 脚本,但我对其中任何一个都不熟悉。
我也
请对这些选项有任何了解的人为我解答这个问题。我正在寻找在速度和内存使用方面最适合的解决方案。
如果有人有其他替代解决方案,我也很高兴听到。
I am making an auction website, which has an auto-bid system. This system lets people make biddings without having to be there.
My question is how to implement such a system. I have made the php files and everything is ready, I've made a html page which refreshes every second. It works, but I'm wondering if there's a better solution.
The second option I tried was making an java application which opens the webpage every second. This consumes "a lot" of memory/CPU. Also, I think there is a memory leak somewhere.
I am running this application on a dedicated linux server with centOS and Plesk, so I am sure there are a lot of alternatives.
For example:
A cronjob that executes a php file every minute, where the php file loops for a minute with sleeps.
One php file that loops with 1 second sleeps.
One cronjob that executes and schedules another cronjob after a second.
MySQL scheduler, but I don't know how to implement that, and I heard it locks the db for other writes.
I also heard about cli, perl, python scripts, but I am not familiar with any of them.
Please can someone with any knowledge of these options please shed some light for me on this subject. I am searching for a solution that is best suited in terms of speed and memory usage.
If someone has another alternative solution I would be glad to hear that also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每一秒都是一个很小的间隔,以至于 cronjobs 不是一个选项,我也强烈建议不要按原样使用“网页”,因为性能很重要,只需为 CLI 编写的脚本就可以做到这一点。
我会做的是:
你应该将其注册在
数据库。
要执行的条目的数据库
那一刻。
无论是谁,您都需要非常小心,让脚本在每个周期后休眠,否则您将关闭数据库。
顺便说一句,您真的需要每秒执行一次吗?在我看来,自动出价是由事件触发的,即在还剩 10 秒时有人出价比你高。这听起来更容易实现,并且占用的资源更少(您也可以在那里使用守护进程,侦听自动出价事件,但那是另一个故事了:)
Every one second is such an small interval that cronjobs are a not an option, also i would strongly suggest against using a "webpage" as is, since performance is important just a script written for the CLI will do it.
What i would do is:
you should register that in a
database.
database for entries to be executed
at that moment.
Whoever, you need to be very careful of making the script sleep after each cycle or you will bring down your database.
On a side note, you really need it to be executed every second? In my mind auto-bids are triggered by events, i.e. someone bidding more than you while there's 10 seconds left. That sound a lot nicer to implement and less resource intensive (and you can also use a daemon there, listening for auto-bids events, but that's another story :)
创建一个 cron 作业,以给定的时间间隔执行相关的 PHP 脚本。您可以从命令行执行 PHP 脚本(如果它是以某种方式编写的)这是与上下文无关的(不需要访问
$_GET
、$_POST
等)。Create a cron job that executes the PHP script in question at a given time interval. You can execute the PHP script from the command line if it's written in a way that is context agnostic (doesn't require access to
$_GET
,$_POST
and similar).好吧,您可以尝试某种“基于事件的发布”,这基本上意味着一旦数据被插入数据库,它就会触发一些事情发生。
举个例子,MySQL 触发器甚至是一个简单的 php 脚本,在插入新出价时,您检查是否有其他人放置了高于该出价的最高出价。
如果他们这样做了,则更新新的最高出价和当前的最高出价者。
如果有多个出价者,您可能需要进行循环并查看谁将获得最高出价。
关键是您不应该真的到处跑并执行预定的工作来解决这个业务问题。我建议您使用触发器或事件。
Well, you could try some sort of "event-based publishing", which basically means that once the data has been inserted into the database, it triggers something to happen.
Case in point, MySQL triggers or even a simple php script that ON INSERT for a new bid, you check if anyone else has placed maximum bid that is higher then that bid.
If they did, then update the new highest bid and the current top bidder.
If there are a number of bidders, you might want to do a loop and see who will get the highest bid.
The point is that you shouldn't really run around and do scheduled jobs to figure out this business problem. I recommend you use triggers or events.
如果您已经有一个页面可以完成这项工作,您可以安排一个 cron 每分钟调用一次curl 命令(curl http:// /your/page.php)。唯一需要注意的是,如果页面被外部调用,您可能需要保护您的页面(如果这是一个问题)。
If you already have a page which does the job, you can schedule a cron that calls a curl command every minute (curl http://your/page.php). The only caveat is that you could need to protect your page if it gets called from outside, should that be a problem.