mirror of
https://github.com/predis/predis.git
synced 2026-08-18 09:42:27 +00:00
184d583895
When using replication backends, now the role of a connection is not defined by its alias but by the new connection parameter "role" that can be set to "master", "slave" and (for redis-sentinel) "sentinel". This also led to a redesign of how connections can be retrieved from replication backends: the method getConnectionById() now retrieves a connection only by its ID (ip:port pair), to get a connection by its alias there is the new method getConnectionByAlias(). This method is not supported by the redis-sentinel backend due to its dynamic nature (connections are retrieved and initialized at runtime from sentinels) but it is still possible to get a single connection from the pool by using its ID. It is also possible to retrive a connection by its role using the method getConnectionByRole(). NOTE: the "role" parameter is an hint for the internals of aggregate connection backends so it is still possible that the actual role of a connection changes during the execution of a script (e.g. a slave gets promoted to the role of master): in this case the parameters of that connection will not be changed as they are immutable, but the method getConnectionByRole() will return the appropriate connection. Predis\Client::getClientBy() has been updated with the addition of "role" and "alias" as supported selectors.
59 lines
2.1 KiB
PHP
59 lines
2.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) Daniele Alessandri <suppakilla@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
require __DIR__.'/shared.php';
|
|
|
|
// Predis supports redis-sentinel to provide high availability in master / slave
|
|
// scenarios. The only but relevant difference with a basic replication scenario
|
|
// is that sentinel servers can manage the master server and its slaves based on
|
|
// their state, which means that they are able to provide an authoritative and
|
|
// updated configuration to clients thus avoiding static configurations for the
|
|
// replication servers and their roles.
|
|
|
|
// Instead of connection parameters pointing to redis nodes, we provide a list
|
|
// of instances of redis-sentinel. Users should always provide a timeout value
|
|
// low enough to not hinder operations just in case a sentinel is unreachable
|
|
// but Predis uses a default value of 100 milliseconds for sentinel parameters
|
|
// without an explicit timeout value.
|
|
//
|
|
// NOTE: in real-world scenarios sentinels should be running on different hosts!
|
|
$sentinels = array(
|
|
'tcp://127.0.0.1:5380?timeout=0.100',
|
|
'tcp://127.0.0.1:5381?timeout=0.100',
|
|
'tcp://127.0.0.1:5382?timeout=0.100',
|
|
);
|
|
|
|
$client = new Predis\Client($sentinels, array(
|
|
'replication' => 'sentinel',
|
|
'service' => 'mymaster',
|
|
));
|
|
|
|
// Read operation.
|
|
$exists = $client->exists('foo') ? 'yes' : 'no';
|
|
$current = $client->getConnection()->getCurrent()->getParameters();
|
|
echo "Does 'foo' exist on {$current->role}? $exists.", PHP_EOL;
|
|
|
|
// Write operation.
|
|
$client->set('foo', 'bar');
|
|
$current = $client->getConnection()->getCurrent()->getParameters();
|
|
echo "Now 'foo' has been set to 'bar' on {$current->role}!", PHP_EOL;
|
|
|
|
// Read operation.
|
|
$bar = $client->get('foo');
|
|
$current = $client->getConnection()->getCurrent()->getParameters();
|
|
echo "We fetched 'foo' from {$current->role} and its value is '$bar'.", PHP_EOL;
|
|
|
|
/* OUTPUT:
|
|
Does 'foo' exist on slave-127.0.0.1:6381? yes.
|
|
Now 'foo' has been set to 'bar' on master!
|
|
We fetched 'foo' from master and its value is 'bar'.
|
|
*/
|