Database · Redis basic to advanced

Keywords: PHP Redis Database Mobile

I. implementation and application of PHP message queue




Advantages and disadvantages of message queuing



III. configuration and preparation of message queue

The cases are as follows:



The following are the team list fields and properties


IV. Mysql order queue

1. Accept the order and write the data order.php

//This file is used to accept the order information of the user and write it to the queue

//Import database link class
include '../include/db.php';
//When receiving the mobile phone number of the user is not empty

  //Here is the processing flow of the order center
  //Because the order system is a separate process that needs to be processed here
  //... it's not written here alone
  //Filter the data submitted by users to prevent sql injection
   //Assume order number directly
  $order_id = rand(10000,99999);
   //Generate order information
    $insert_data = array(
         'created_at'=>date('Y-m-d H:i:s',time()),
         'status'=>0,//0 Unprocessed 1 processed 2 processing 
    //Store order information in team list
    $db = DB::getIntance();//Here is the operation database. Note that different frameworks have corresponding built-in operation methods
   $res =  $db->insert('order_queue',$insert_data);//insert($table,$data)
    echo  'Insert success';

    echo 'Insert failure';


2. The queue system processes the orders in the queue and identifies goods.php

//This file is mainly a file that the distribution system processes and marks the orders in the queue
//Load database operation class
include '../include/db.php';
$db = DB::getIntance();
//1: First, update the records to be processed to wait for processing (here, lock the order to prevent it from being used before the processing is completed)
$waiting = array('status'=>0);//Data waiting to be processed
$lock = array('status'=>2);//Locked data
$res_lock = $db->update('order_queue',$lock,$waiting,2);//update($table,$data,$where,$limit=0)
//2: select the data just updated, and then process the distribution system
    //Select the content of the order to be processed 
     $res = $db->selectAll('order_queue',$lock);//
    //Then the distribution system will handle the distribution
    //... / / this is the distribution system 
   //3: update the processed program to completed
   $success = array(
        'updated_at'=>date('Y-m-d H:i:s',time()),
    $res_last = $db->update('order_queue',$success,$lock);
         echo 'Success'.$res_last;
         echo 'Fail'.$res_last;

    echo 'ALL Finished';

3. Scheduled tasks. Execute once a minute to understand the shell foundation


date "+%G-%m-%d %H:%M:S"
cd /home/html/ssggw/queue_mysql/
php goods.php

4. Schedule task crontab-e under server deployment

For the convenience of viewing the results, output the timing task results to the log file and set it as standard output. The command is as follows


Execute command to save file: wq

New log file command

 touch /home/html/ssggw/log.log


Monitoring log file

Two pieces of data in the database have been successful

Four pieces of data in the database have been successful


End of decoupling case

Five, Redis



Vi. Redis queue achieves second kill

1. Create a redis queue

2. The program user.php that accepts the user's request

//Load first redis assembly
$redis = new Redis();
$redis_name = "miaosha";

//Accept users id
$uid = $_GET['uid'];
//Get down redis Quantity already in it
$num = 10;//Top 10 seconds
//If the number of people in the day is less than 10, join the queue
   $redis->rFush($redis_name,$uid.'%'.microtime());//Users uid(Connect a subtle) join $fredis_name The end of the queue
   echo "Spike kill";
  //If the number of people on the day has reached 10, the second kill is completed 
   echo "The second kill is over";

//turn off redis link

3. The handler, that is, the report data is written to the data table savetodb.php corresponding to mysql

//Introduce operation database class
include '../include/db.php';

//Load redis assembly
$redis = new Redis();
$redis_name = "miaosha";

$db = DB::getlntance();

//Dead cycle
   //Take a value from the far left of the queue
   $user = $redis->IPop($redis_name);
   //Then determine whether the value exists
   if(!$user || $user =='nil'){
       //Reduce the pressure on the server. The real time to execute the second kill every 1 second is very short. No need to set it
       //Jump out of circulation
  //Cut out time, uid
  $user_arr = explode('%',$user);
      //Define an array
   $insert_data = array(
   //Save to database
    $res = $db->insert('redis_queue',$insert_data);
   //Rollback mechanism in case of database insertion failure
//Release it. redis



Access the user.php file url


Database table status

Posted by NNTB on Mon, 02 Dec 2019 22:34:26 -0800