[thinkphp5 operation redis series tutorial] pconnect (or popen) connection

1.pconnect

    /**
     * Connects to a Redis instance or reuse a connection already established with pconnect/popen.
     *
     * The connection will not be closed on close or end of request until the php process ends.
     * So be patient on to many open FD's (specially on redis server side) when using persistent connections on
     * many servers connecting to one redis server.
     *
     * Also more than one persistent connection can be made identified by either host + port + timeout
     * or host + persistent_id or unix socket + timeout.
     *
     * This feature is not available in threaded versions. pconnect and popen then working like their non persistent
     * equivalents.
     *
     * @param string    $host          can be a host, or the path to a unix domain socket
     * @param int       $port          optional
     * @param float     $timeout       value in seconds (optional, default is 0 meaning unlimited)
     * @param string    $persistent_id identity for the requested persistent connection
     * @return bool                    TRUE on success, FALSE on ertcnror.
     */
    public function pconnect( $host, $port = 6379, $timeout = 0.0, $persistent_id = null ) {}

2. Without port

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        $con = $redis->pconnect('127.0.0.1');
        var_dump($con);//bool(true)

    }

}

2. port

 

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        $con = $redis->pconnect('127.0.0.1',6379);
        var_dump($con);//bool(true)

    }

}

3. band timeout

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        // Give up for more than 3 seconds
        $con = $redis->pconnect('127.0.0.1',6379,3);
        var_dump($con);//bool(true)

    }

}

4.x is sent as a persistent "Id" and is the previous three connections.

$redis->pconnect('127.0.0.1', 6379, 2.5, 'x'); 

5.nix domain socket - will be the previous four connections.

$redis->pconnect('/tmp/redis.sock'); 

6. Difference between pconnect and connect

After the connect script ends, the connection is released

After the pconnect script ends, the connection is not released and saved in the PHP FPM process

Connect when a large number of php requests come to redis, this method will continuously establish and close the connection, which takes up a lot of resources, resulting in a large number of timew ﹣ wait on the server, so there will be a lot of php requests that cannot connect to redis, and there will be the anomaly of redis server wet away, which actually indicates that the redis service has not received the request.

The connection established by pconnect method does not close with the end of the request, but depends on the PHP FPM process. The PHP FPM process does not die, and redis connect always exists until the idle timeout is automatically disconnected (currently redis timeout 600), which greatly improves the reuse of redis connect and reduces a lot of IO overhead. However, there is a flaw. The number of redis connect clients is much higher than before

Keywords: Redis PHP socket Unix

Added by smith.james0 on Thu, 02 Jan 2020 11:10:13 +0200