Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any php redis client support persistent connection?

As the title, I'm looking for a php Redis client that support persistent connection, because my web application receives a lot of requests(each request, it'll put an item in to Redis queue) and I want to avoid create new connection every request.

like image 559
secmask Avatar asked Sep 01 '10 03:09

secmask


People also ask

Are Redis connections persistent?

Using persistent connections to Redis means that the connection is not closed until the PHP process dies. So if your PHP-FPM pool creates a lot of PHP processes then theses ones can create more connections than your Redis server is able to handle and this will trigger maximum number of clients reached errors.

What is persistent connection in PHP?

Persistent Database Connections ¶ Persistent connections are links that do not close when the execution of your script ends. When a persistent connection is requested, PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it.

Does Redis have connection pool?

Redis-py provides a connection pool for you from which you can retrieve a connection. Connection pools create a set of connections which you can use as needed (and when done - the connection is returned to the connection pool for further reuse).

Do we need to close Redis connection?

Due to the single-threaded nature of Redis, it is not possible to kill a client connection while it is executing a command. From the client point of view, the connection can never be closed in the middle of the execution of a command.


1 Answers

Predis supports persistent connection. you just need to add persistent paramater as 1.

you can use the code below

$client = new Predis\Client(array(
   'scheme'    => 'tcp',
   'host'      => '127.0.0.1',
   'port'      => 6379,
   'database'  => 15,
   'persistent'=> 1
));

instead of

$client = new Predis\Client('tcp://127.0.0.1:6379?database=15');

you can find more parameters for the connection here : https://github.com/nrk/predis/wiki/Connection-Parameters

like image 71
Hanee Park Avatar answered Sep 28 '22 21:09

Hanee Park