秒殺訂單提前寫入
header('Content-type:text/html;charset=utf-8');
//實例化Redis
$redis = new Redis();
// 連接redis
$redis -> connect('localhost',6379);
// 如果redis開啟了安全驗證必須驗證密碼才能進入
$redis -> auth('123456');
$redis_name = 'miaosha';
$i = 500;
$n = 0;
for ($i=500; $i >= 0; $i--) {
$uid = mt_rand(1000000,9999999);
$redis -> rpush($redis_name,$uid);
}
echo '秒殺隊列生成成功';
秒殺搶購取出
header('Content-type:text/html;charset=utf-8');
//實例化Redis
$redis = new Redis();
// 連接redis
$redis -> connect('localhost',6379);
// 如果redis開啟了安全驗證必須驗證密碼才能進入
$redis -> auth('123456');
$redis_name = 'miaosha';
if ($uid = $redis->lpop($redis_name)) {
$redis -> rpush('order',$uid);
echo $uid;
}else{
var_dump($uid);
}