Why are the php clients of redis so connected?

  ios, question

I used redis as the caching system in the website and used its pecl client (this is ithttps://github.com/nicolasff/phpredis)。 There is nothing wrong with using it, but I found a problem when I looked at the number of links recently, that is, the number of background links is extremely high.

链接数截屏

As can be seen from the above figurenetstat -na | grep 6379After the port occupancy situation, in fact, there are several screens, I will not post. What I want to ask is, is this a bug of the client or is it itself? If it is like this, will there be any problem if it is occupied like this, or will it have an upper limit of its own? Because I didn’t see its configuration or initialization parameter inside has connection pool settings, so I can be sure that this should not be the hold state of connection pool.

I see that its connection methods areConnectAndpconnectI am currently usingConnectBecause the reference pairmysql_pconnectThe number of requests is not very large now, so there is no need to use it.ConnectAnd it should also release the connection after the script ends. The current situation is that these connections seem to be automatically disconnected only after the server side of redis has timed out for 30 seconds.

I’m worried that if the number of requests increases in the future, it will burst in 30 seconds.

I also use this plug-in, after my test, this is definitely a cheating design of this redis client! Because it has to close the connection itself, closing the connection is an almost forgotten operation for php developers.

Because the developers of the general extension will close the used connection by themselves at the end of the script, but this plug-in doesn’t have it, and you need to close it yourself. Of course, closing the connection pair is only rightConnectThe function is valid.pconnectThere is no need to close the connection. The specific use method is to call the close method of redis object before the script ends to close the connection. Or more convenient, use code similar to the following

register_shutdown_function(function () {
    global $redis;
    $redis->close();
});

After testing, close the connection by yourself, usenetstat -naSee 6379 port connection is very few.