secmask
secmask

Reputation: 8117

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.

Upvotes: 11

Views: 18114

Answers (5)

Hanee Park
Hanee Park

Reputation: 125

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

Upvotes: 3

Robert Calhoun
Robert Calhoun

Reputation: 5143

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.)

Upvotes: 4

robbie613
robbie613

Reputation: 675

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',
    ),
);

Upvotes: 0

Andrew McKnight
Andrew McKnight

Reputation: 644

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.

Upvotes: -2

antirez
antirez

Reputation: 18504

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.

Upvotes: 9

Related Questions