php new predisclient,PHP Redis Client and Replica

本文介绍了如何在PHP中配置Redis客户端以实现主从复制,确保写操作在主节点执行,读操作在从节点执行。通过设置两个独立的客户端对象,一个用于写操作,一个用于读操作,解决了单个连接对象一旦连接到主节点后不会切换的问题。更新到Predis v1.1.1以支持这些特性,并提供了composer.json配置文件示例。
摘要由CSDN通过智能技术生成

PHP Redis Client and Replica We can configure the replica connection client like this: $timeout_parameters = "&timeout=10&read_write_timeout=10"; $parameters = array(      'tcp://' . $redisHostMaster . "?alias=master&$timeout_parameters",      'tcp://' . $redisHostSlave1 . "?alias=slave1&$timeout_parameters",      'tcp://' . $redisHostSlave2 . "?alias=slave2&$timeout_parameters" ); $this->clientMaster = new Client($parameters, array('replication' => true)); If we create a new Client every time we use it, it will be good. It can send all the writes to master, all the reads to replica. But if we use a single object of that connection, once it connect to master, it will be master forever. That is what I get from the testings. So finally, my solution change to this, I will keep 2 singleton clients, one for all writes, one for all reads. $timeout_parameters = "&timeout=10&read_write_timeout=10"; $parameters = array(      'tcp://' . $redisHostMaster . "?alias=master&$timeout_parameters",      'tcp://' . $redisHostSlave1 . "?alias=slave1&$timeout_parameters",      'tcp://' . $redisHostSlave2 . "?alias=slave2&$timeout_parameters" ); $this->clientMaster = new Client($parameters, array('replication' => true)); $this->clientSlaves = new Client($parameters, array()); And we upgrade the client to v.1.1.1 to support all these. composer.json is as follow: { "autoload" : {         "psr-0": {             "JobProducerPHP": "src/"         }     },     "require": {         "predis/predis": "1.1.1",         "aws/aws-sdk-php": "3.0.6",         "katzgrau/klogger": "dev-master",         "pimple/pimple": "3.0",         "solarium/solarium": "^3.6",         "phpFastCache/phpFastCache": "^4.3"     },     "require-dev": {         "php": ">=5.3.0",         "phpunit/phpunit": "~4.5.1",         "phpunit/dbunit": ">=1.2",         "phpunit/php-invoker": "*"     } } We need to execute command as follow: > ./composer.phar update References: https://github.com/nrk/predis/tree/v1.1/examples https://github.com/nrk/predis/blob/v0.7.1/examples/MasterSlaveReplication.php https://github.com/nrk/predis/issues/21 https://github.com/nrk/predis/wiki/Connection-Parameters cluster https://www.zybuluo.com/phper/note/248555

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值