PHP commodity seconds to kill timing implementation (to solve large traffic scenarios)

Source: Internet
Author: User
Tags flock

php Commodity seconds kill function we mostly take the whole point or time as an example, so for PHP, processing is not complex, but there is a problem is if the traffic is how to deal with, let's look at the solution. Requires the display of a real-time countdown of hours minutes seconds, and the user-side modification date time does not affect the normal display of the countdown (that is, the server time). In fact, this and a lot of examinations and other systems of time limit function the same requirements. It is not always possible to use Ajax to get server time every second, so the real-time countdown must be implemented with JavaScript. This is simple, a bunch of examples online. The problem now is to address the effect of the user-side modification date time on our display. The solution is to calculate the time between the client and the server, so the problem is solved. This only needs to run PHP once, and the time of the real-time countdown is synchronized with the server's time. The theory is synchronous, but the actual test will have a 1 second error. (The specific reason is connected with the speed, the faster the speed, the smaller the error), but this will not affect our requirements above. Note: The seconds kill time from morning to evening 10 o'clock. Code is as follows:<?PHP//the time in PHP is calculated in seconds. JS time is counted in millisecondsDate_default_timezone_set ('PRC'); //date_default_timezone_set ("Asia/hong_kong");//Area//Configure the active time period for each day$starttimestr ="08:00:00"; $endtimestr="22:00:00"; $starttime=Strtotime ($STARTTIMESTR); $endtime=Strtotime ($ENDTIMESTR); $nowtime=Time ();if($nowtime <$starttime) {Die ("activity has not started, the active time is: {$starttimestr} to {$endtimestr}"); } $lefttime= $endtime-$nowtime;//The actual time remaining (seconds)? ><script language="JavaScript"> </script>"Remainh">xx</strong>:<strong id="REMAINM">xx</strong>:<strong id="remains">XX</strong>There's no problem with that, but there's a number of incorrect problems in the traffic conference, such as the problem of negative inventory caused by large traffic and storage. We know that database processing of SQL is a process, assuming that the process of purchasing goods is this: SQL1: Check Commodity inventoryif(Stock quantity >0) {   //Generate Order ...SQL2: Stock-1when there is no concurrency, the above process seems to be so perfect, assuming that at the same time two people order, and inventory only 1, in the SQL1 stage, two people to check the inventory is>0, and then finally executed the SQL2, inventory finally changed-1, oversold, or fill inventory, or other user complaints. To solve this problem more popular ideas:1. With an extra single process to process a queue, placing a request in the queue, processing each, there will be no concurrency problem, but the additional background process and delay issues, not considered. 2Database optimistic lock, roughly means to check inventory first, and then immediately put inventory +1, and then after the order is generated, check the inventory again before updating the inventory, and see if the expected inventory quantity is consistent, the inconsistency rolls back, prompting the user for insufficient inventory. 3According to the update results, we can add a judging condition to update the SQL2.whereStock >0If False is returned, the inventory is insufficient and the transaction is rolled back. 4. with file exclusive lock, when processing the order request, lock a file with flock, if the lock failure indicates that there are other orders being processed, either wait or prompt the user directly"Server Busy"This article is to say the 4th scenario, the approximate code is as follows:
Block (wait) mode
<?php $fp = fopen ("Lock.txt", "w+"); if (Flock ($FP, lock_ex)) {   //: Processing order   Flock ($FP, Lock_un);} fclose ($fp);?>
 
Non-blocking mode
<?php $fp = fopen ("Lock.txt", "w+"); if (Flock ($fp, LOCK_EX | LOCK_NB)) {   //.. Processing order   Flock ($FP, Lock_un);} else{   echo "The system is busy, please try again later";}   Fclose ($FP);?>

PHP commodity seconds to kill timing implementation (to solve large traffic scenarios)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.