PHP Redis Client and Replica

PHPRedisClientandReplica

Wecanconfigurethereplicaconnectionclientlikethis:

$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=newClient($parameters,array('replication'=>true));

IfwecreateanewClienteverytimeweuseit,itwillbegood.Itcansendallthewritestomaster,allthereadstoreplica.Butifweuseasingleobjectofthatconnection,onceitconnecttomaster,itwillbemasterforever.ThatiswhatIgetfromthetestings.

Sofinally,mysolutionchangetothis,Iwillkeep2singletonclients,oneforallwrites,oneforallreads.

$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=newClient($parameters,array('replication'=>true));

$this->clientSlaves=newClient($parameters,array());

Andweupgradetheclienttov.1.1.1tosupportallthese.composer.jsonisasfollow:

{

"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":"*"

}

}

Weneedtoexecutecommandasfollow:

>./composer.pharupdate

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

相关推荐