Home > Backend Development > PHP Tutorial > In-depth php redis pconnect_PHP tutorial

In-depth php redis pconnect_PHP tutorial

WBOY
Release: 2016-07-13 09:45:56
Original
1220 people have browsed it

In-depth php redis pconnect

pconnect, the API used by clients to connect to servers in phpredis.

The connection will not be closed on close or end of request until the php process ends.
This is the original sentence in the api description

Then the question is:
1. Does php process ends refer to the completion of a PHP execution or the end of FPM? If it is the latter, it means that even if one PHP execution is completed, the redis connection will not be released, and the redis connection will be reused the next time it is executed.
2. The connection will not be closed on close means that if pconnect is used, even if close() is called in the code, the connection will not be closed?

With these two questions, let’s do an experiment and take an in-depth look at what pconnect does.

Preparation

Environment:
nginx fpm
php5.3
We configure fpm as

<code avrasm="" class="hljs">pm.max_children = 1
pm.start_servers = 1
pm.max_spare_servers = 1</code>
Copy after login

In this way, our page request will be executed by a certain fpm process, which is convenient for strace tracking.

PHP code corresponding to page request:

<code class="language-php" hljs="">$ip = 10.136.30.144; 
$port = 7777; 
$redis = new Redis();

$redis->pconnect($ip, $port, 1);
$key = test;
$value = this is test;

$redis->set($key, $value);
$d = $redis->get($key);
var_dump($d);</code>
Copy after login

The function of the code is very simple. Connect to redis, set a value first, and then take it out.

Test Question 1

Things:
Use strace to observe the system call of fpm. If the life cycle of the connection is one PHP execution, then every time the page is called, there will be a connect system call to connect to redis; if the life cycle of the connection is the end of fpm, then only the first time There will be a connect system call when the page is called. Since the connection is reused later, there is no need to connect, just send the command request directly.

Start a new fpm (process number 28082).
Execute

<code class="language-shell" hljs="" lasso="">strace -p 28082 -s 1024 -o redis_1</code>
Copy after login

Record the system call of a page request. As shown below:
In-depth php redis pconnect_PHP tutorial
You can see that the process first established a socket connection (file descriptor is 9). Then send a series of commands to reids, and finally get the result string of "this is test". And there is no redis command or system call related to closing the connection. <喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwPtKzw bH68fzveHK Lrzo6zO0sPH1rTQ0DwvcD4NCjxwcmUgY2xhc3M9"brush:java;"> lsof -n -p 28082

In-depth php redis pconnect_PHP tutorial
It can be seen that the fpm process still maintains a reids connection to 10.136.30.144, and its file descriptor is 9 (this is consistent with the result of strace).

Execute

<code class="hljs" lasso="">strace -p 28082 -s 1024 -o redis_2</code>
Copy after login

Record the system call of the second page request and get the following results.
In-depth php redis pconnect_PHP tutorial
The difference from the first request is that the process of establishing a connection is omitted, and the reids command is sent directly to get the result!
Then use lsof -n -p 28082 to view the file descriptor opened by fpm. The result is the same as the above file.
It shows that the connection is indeed reused and not newly created.

Execute the 6th page request (because of our configuration in the preparation work, fpm is already a new process at this time), and use lsof to view the file descriptors opened by the process.
In-depth php redis pconnect_PHP tutorial
We found that although there is still a reids connection with descriptor 9, the temporary ports of the two tcp connections are different, that is, the connection has changed!

At this point, we come to the conclusion of question 1:
When using pconnect, the connection will be reused. The life cycle of the connection is the life cycle of the fpm process, not a PHP execution. .

Test Question 2

For comparison, let’s take a look first, use connect to connect to redis, and call the redis->close() system call. (Change pconnect to connect in the above code, and add redis->close() at the end)
In-depth php redis pconnect_PHP tutorial
We see that in addition to establishing the connection, at the end of the program, the quit command is sent to reids and the connected file descriptor is closed.

Next, let’s see what the performance of redis->close() is after using pconnect? http://www.Bkjia.com/kf/yidong/wp/" target="_blank" class= "keylink">WPGJyIC8 DQq0 sLrtffV 86qo7o8L3A DQo8cHJlIGNsYXNzPQ=="brush:java;"> $ip = 10.136.30.144; $port = 7777; $redis = new Redis(); $redis->pconnect($ip, $port, 1); $key = test; $value = this is test; $redis->set($key, $value); $d = $redis->get($key); var_dump($d); $redis->close(); try{ $redis->get($key); } catch (Exception $e){ echo $e->getMessage(); }

Let’s look directly at the system call for executing the page request for the second timeIn-depth php redis pconnect_PHP tutorial
No connection is established, and the command is sent directly to get the result. Indicates that the connection is reused. At the same time, no quit command is sent to the reids server, and there is no system call to close the connection.
But note that the return result of the page request:
In-depth php redis pconnect_PHP tutorial
At this point, we come to the conclusion of question 2:
If pconnect is used in the code, the function of close is only to prevent the current PHP from making redis requests, but it cannot actually close the long redis connection. The connection will still be reused in subsequent requests until the end of the fpm process life cycle.

Conclusion

1. When using pconnect, the connection will be reused. The life cycle of the connection is the life cycle of the fpm process, not one PHP execution.
2. If pconnect is used in the code, the function of close is only to prevent the current PHP from making redis requests, but it cannot actually close the long redis connection. The connection will still be reused in subsequent requests until the end of the fpm process life cycle.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1036920.htmlTechArticleDeep into php redis pconnect pconnect, the API in phpredis for client to connect to server. The connection will not be closed on close or end of request until the php process ends. This is what the api says...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template