Question

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.

Was it helpful?

Solution

Not sure if this is supported but you should definitely look at Predis and Rediska, this two (especially Predis AFAIK) are the best PHP Redis clients available.

OTHER TIPS

PhpRedis currently supports persistent connections. Using PHP 7.0 and PhpRedis 3.0, making a persistent connection with pconnect() like this:

for ($i=0;$i<1000;$i++) {
    $redis = new Redis();
    $result = $redis->pconnect('127.0.0.1'); 
    $redis->set("iterator",$i);
    $response=$redis->get("iterator");
    $redis->close();
    unset($redis);
}

is about 10 times faster (9.6 msec vs 0.83 msec per connection) than connect():

for ($i=0;$i<1000;$i++) {
    $redis = new Redis();
    $result = $redis->connect('127.0.0.1'); 
    $redis->set("iterator",$i);
    $response=$redis->get("iterator");
    $redis->close();
    unset($redis); 
}

Note: "This feature is not available in threaded versions". (I'm running under IIS on Windows, so I run the NTS version.)

Predis supports persistent connections using it's PhpiredisStreamConnection with the persistent=1 flag syntax since v0.8.0:

<?php
$client = new Predis\Client('tcp://127.0.0.1?persistent=1', array(
    'connections' => array(
        'tcp'  => 'Predis\Connection\PhpiredisStreamConnection',
        'unix' => 'Predis\Connection\PhpiredisStreamConnection',
    ),
);

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

PHP-Redis supports persistent connections since it uses a php extension written in C which gives it a mechanism for sharing connections between requests. Look at the documentation on popen and pconnect.

Predis cannot support persistent connections because it is 100% PHP and PHP shares nothing between each request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top