正如标题,我正在寻找支持持久连接一个php Redis的客户,因为我的web应用程序接收大量的请求(每个请求,它会让一个项目中的Redis队列)的和我想避免创建新的连接的每个请求。

有帮助吗?

解决方案

不知道这是否是支持的,但你一定要看看Predis和Rediska,这两年(尤其是Predis据我所知)是最好的PHP Redis的客户提供。

其他提示

PhpRedis 目前支持持久连接。使用PHP 7.0和3.0 PhpRedis,使得与 pconnect() 这样的持久连接:

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);
}

为约10倍的速度比 connect() (9.6毫秒VS 0.83每个连接毫秒) :

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); 
}

注:“此功能在线程版本”。 (我运行IIS下在Windows上,所以我运行NTS版本。)

Predis支持使用它的PhpiredisStreamConnection以来v0.8.0的persistent=1旗标语持久连接:

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

Predis支持持久连接。你只需要添加持久paramater为1。

可以使用下面的代码

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

,而不是

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

您可以从这里找到更多的连接参数:   https://github.com/nrk/predis/wiki/Connection-Parameters

PHP-Redis的支持持久连接,因为它使用PHP扩展用C语言编写这给它的请求之间共享连接的机构。看的文档上 POPEN和pconnect

Predis的不能的支持持久连接,因为它是每个请求之间100%PHP和PHP股什么。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top