美文网首页
PHP操作Redis集群

PHP操作Redis集群

作者: Uzero | 来源:发表于2017-03-20 10:20 被阅读0次

首先php连接redis集群有两个扩展:predis 和 phpredis

predis:纯php开发,使用了命名空间,需要php5.3+,灵活性高

phpredis:这是个c扩展,性能更高,但是phpredis2.x扩展不行,需升级phpredis到3.0才能连接集群

这里使用phpredis

一、安装phpredis

phpredis下载地址:https://github.com/nicolasff/phpredis

unzip phpredis-develop.zip

cd phpredis-develop

/usr/local/PHP/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config

中途有可能需要安装gcc

yum install gcc即可

make

make install

vi /usr/local/php/etc/php.ini

加入 extension=redis.so

apache下重启httpd或apache,NGINX下重启php-fpm即可

checking for egrep... /usr/bin/grep -E

checking for a sed that does not truncate output... /usr/bin/sed

checking for cc... no

checking for gcc... no

checking for egrep... /usr/bin/grep -E

checking for a sed that does not truncate output... /usr/bin/sed

checking for cc... no

checking for gcc... no

checking for egrep... /usr/bin/grep -E

checking for a sed that does not truncate output... /usr/bin/sed

checking for cc... no

checking for gcc... no

二、连接使用

try{

       $obj_cluster = new RedisCluster(NULL, ['192.168.5.155:6379', '192.168.5.155:6378','192.168.5.155:6377', '192.168.5.155:6376', '192.168.5.155:6375'],1.5,1.5);

}catch(Exception $e){

        echo $e->getMessage();

}

$obj_cluster -> set('name', 'yuanbl');

$obj_cluster -> set('team', 'happy');

$obj_cluster -> set('part', 'service');

$name1 = $obj_cluster -> get('name');

$name2 = $obj_cluster -> get('team');

$name3 = $obj_cluster -> get('part');

echo $name1.'--'.$name2.'--'.$name3;

打印:yuanbl--happy--service

三、连接参数说明

第一个参数 : NULL  文档上没找到为什么是NULL

第二个参数 : 连接的redis cluster的master服务器列表。

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

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

四、git地址

https://github.com/phpredis/phpredis/blob/feature/redis_cluster/cluster.markdown

相关文章

网友评论

      本文标题:PHP操作Redis集群

      本文链接:https://www.haomeiwen.com/subject/iisbnttx.html