English 中文(简体)
Automatic Database reads/write every second
原标题:

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:

  1. A cronjob that executes a php file every minute, where the php file loops for a minute with sleeps.

  2. One php file that loops with 1 second sleeps.

  3. One cronjob that executes and schedules another cronjob after a second.

  4. MySQL scheduler, but I don t know how to implement that, and I heard it locks the db for other writes.

  5. 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.

最佳回答

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:

  • When someone activates the auto-bid you should register that in a database.
  • Create a Daemon which polls the 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 :)

问题回答

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).

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.

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.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签