Home  >  Article  >  Backend Development  >  How does php+redis+mysq handle high concurrency (example code)

How does php+redis+mysq handle high concurrency (example code)

不言
不言Original
2018-08-06 14:11:432001browse

The content of this article is about how php redis mysq handles high concurrency (example code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Experimental environment
ubuntu, php, apache or nginx, mysql
2. Requirements
Now there is an interface that may appear When the amount of concurrency is relatively large, this interface is written in PHP. Its function is to receive the name field in the user's GET request, and then store this field in mysql. Now first put the data into the redis queue, and then let Redis regularly transfers these data to mysql.
2. Implementation steps
1. Create a new database test and data table test. The table creation statements are as follows

CREATE TABLE `test` (
  `name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf-8

1. Create a new index in /var/www/test. php, the content is as follows, and configure the virtual host to make it accessible.

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
try {
    $res = $redis->LPUSH('name', $_REQUEST["name"]);
} catch (Exception $e) {
    echo $e->getMessage();
}

2. Create a new redis.php file in the same directory, and pay attention to modifying the database password and other configurations. The content is as follows

<?php
$redis = new Redis();
$redis->pconnect('127.0.0.1',6379);
$mysql=mysqli_connect("localhost","root","bnm");
mysqli_select_db($mysql,"test") or die("不能选择数据库");
if(!$mysql){
    die("连接失败");
}
while (true){
    try{
        $value = $redis->LPOP('name');
        if(!$value){
            echo "等待";
        }else{
            $sql="insert into test(name) values ('".$value."')";
            $result=mysqli_query($mysql,$sql);
            if($result&&mysqli_affected_rows($mysql)>0){
                echo "插入成功";
            }else{
                echo "插入失败:".mysqli_error($mysql);
            }
        }
    }catch(Exception $e){
        echo $e->getMessage();
    }
    sleep(1);
}

3. Run the redis.php script file

nohup php redis.php &

4. Visit the index.php script file, such as: http://192.168.116.128/?name=33, and then check whether the data has been entered in mysql.

Recommended related articles:

What are the operations of PHP array functions? Application of php array function (with code)

thinkphp5 framework and Android implement QR code generation code

The above is the detailed content of How does php+redis+mysq handle high concurrency (example code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn