php+redis消息隊列是php+mysql性能不足時的一箇中間間處理方案。經過這個中間的處理,保證的數據的可用性和準確性。用於服務器瞬間請求大,數據庫壓力大的狀況。如併發量大致使的超賣、併發量大致使的數據重複狀況。php
流程:php接受請求和數據 -> php把數據寫入redis隊列中(入隊) -> shell定時調用php讀取隊列數據寫入mysql(出隊)html
實現代碼:mysql
入隊:inqueue.phpnginx
<?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]; //這裏能夠是get或post請求過來的數據
$data = json_encode($data);
$in = $redis->rpush('queue',$data);
if($in){
echo "入隊成功";
}
出隊:outqueue.phpredis
#!/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);
shell process.sh:定時調用outqueue.php腳本sql
#進行每分鐘調用一次
* * * * * /usr/local/nginx/html/process.sh
#!/bin/bash
#file_name : process.sh
#author : zuoping
php /usr/local/nginx/html/outqueue.php
*若是每分鐘調用一次頻率不夠,能夠執行屢次調用腳本,如:shell
#!/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
#這樣就一分鐘調用了屢次了。
查看隊列中的當前數據:數據庫
<?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);