PHP + Redis message queue is an intermediate solution when PHP +mysql performance is inadequate. Through this intermediate processing, ensure the availability and accuracy of the data. It is used when the server has a large instant request and the database is under great pressure. Such as oversold caused by a large amount of concurrency, data duplication caused by a large amount of concurrency.
-> Shell periodically calls PHP to read the queue and write the data to mysql (dequeue).
Implementation code:
Team: inqueue. PHP
<? php$redis = new redis();
$redis->connect('127.0.0.1', 6379);$redis -> select('1');
$redis->auth(' ');
$data= [a,b,c,d,e,f,g,h]; // This can be a get or post request$data = json_encode($data);
$in = $redis->rpush('queue'.$data);
if($in) {echo "Made the team.";
}
Copy the code
The team: outqueue. PHP
#! /usr/bin/php<? php$redis = new redis();
$redis->connect('127.0.0.1', 6379);$redis -> select('1');
$redis->auth(' ');
$value = $redis->lpop('queue');
$value = json_decode($value.true);
Copy the code
Shell process.sh: Invokes the outqueue.php script periodically
Make a call per minute
* * * * * /usr/local/nginx/html/process.sh
#! /bin/bash
#file_name : process.sh
#author : zuoping
php /usr/local/nginx/html/outqueue.phpCopy the code
* If one call per minute is not enough, you can execute multiple call scripts, such as:
#! /bin/bash
#file_name : process.sh
#author : zuoping
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
php /usr/local/nginx/html/outqueue.php
This is multiple calls per minute.Copy the code
To view the current data in the queue:
<? php$redis = new redis();
$redis->connect('127.0.0.1', 6379);$redis -> select('1');
$redis->auth(' ');
$list = $redis->lrange('queue', 0, 1); var_dump($list);
Copy the code
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points are free to share with you