phpredis 与 predis

原博文链接:https://www.cnblogs.com/afeige/p/14385588.html

区别:

在性能上的区别当然是扩展更好一些,但其实这两个实现还有更大的区别,就是连接的保持。

  1. phpredis在扩展中使用c可以保持php-fpm到redis的长连接,所以一个php-fpm进程上的多个请求是复用同一个连接的。phpredis的pconnect就是长连接方式。
  2. predis是使用php的socket来连接redis,所以需要每次请求连接redis。

可以看出laravel的官方是推荐使用predis的,因为纯php实现的原因,只需要composer即可安装,非常符合laravel便捷的思想。

phpredis 和 predis 的性能差距没有跨数量级,当然要考虑具体业务,如果业务非常依赖redis,并且单机qps需要支持的比较大,建议使用phpredis。如果你只是使用laravel使用redis实现规模小的业务,建议不用改变predis。

[PHPRedis]
  1. 单机方式
<?php
$client = new Redis();
$client->connect('192.168.1.1', '6379');
echo $client->get('cache_key:oneKey');

  2. 集群方式

<?php
$redis_list = ['192.168.1.1:6379','192.168.1.2:6379','192.168.1.3:6379'];
$client = new RedisCluster(NUll,$redis_list);
echo $client->get('cache_key:oneKey');

  3. 超时设置

<?php
$redis_list = ['192.168.1.1:6379','192.168.1.2:6379','198.168.1.3:6379', 1.5, 1.5];
$client = new RedisCluster(NUll,$redis_list);
echo $client->get('cache_key:oneKey');

timeout和read_timeout功能。就是加到master列表的后面。

timeout表示连接redis的最长时间,这里设为1.5秒,表示超过1.5秒要是还没连接成功就返回false 。

read_timeout表示连接redis成功后,读取一个key的超时时间,有时候读取一个key 可能value比较大,读取需要很长时间,这里设置1.5秒,表示要是过了1.5秒还没读取到数据就返回false。


  4. 扩展安装

~ git clone git@github.com:phpredis/phpredis.git

~ cd phpredis

~ git fetch

~ git checout feature/redis_cluster #切换到cluster分支

~ phpize

~ ./configure

~ make

~ make install
 
 
[predis]

  1. 连接方式(集群)

<?php
use PredisClient;
require __DIR__ . '/../vendor/autoload.php';

// 写一个节点也可以
$redis_list = [
        'tcp://192.168.1.1:6379',
        'tcp://192.168.1.2:6379',
        'tcp://192.168.1.3:6379'
];

$redis = new Client($redis_list, ['cluster'=>'redis']);

echo $redis->get('cache_key:oneKey');

   2. 连接方式(单机)

PredisAutoloader::register();
$client = new PredisClient([
        'scheme' => $scheme,
        'host'   => $host,
        'port'   => $port,
]);
原文地址:https://www.cnblogs.com/csjboke/p/14440074.html