Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of the Predis package.
5
 *
6
 * (c) Daniele Alessandri <suppakilla@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

    
12
require 'SharedConfigurations.php';
13

    
14
// Predis supports master / slave replication scenarios where write operations are
15
// performed on the master server and read operations are executed against one of
16
// the slaves. The behaviour of commands or EVAL scripts can be customized at will.
17
// As soon as a write operation is performed, all the subsequent requests (reads
18
// or writes) will be served by the master server.
19
//
20
// This example must be executed with the second Redis server acting as the slave
21
// of the first one using the SLAVEOF command.
22
//
23

    
24
$parameters = array(
25
    'tcp://127.0.0.1:6379?database=15&alias=master',
26
    'tcp://127.0.0.1:6380?database=15&alias=slave',
27
);
28

    
29
$options = array('replication' => true);
30

    
31
$client = new Predis\Client($parameters, $options);
32

    
33
// Read operation.
34
$exists = $client->exists('foo') ? 'yes' : 'no';
35
$current = $client->getConnection()->getCurrent()->getParameters();
36
echo "Does 'foo' exist on {$current->alias}? $exists.\n";
37

    
38
// Write operation.
39
$client->set('foo', 'bar');
40
$current = $client->getConnection()->getCurrent()->getParameters();
41
echo "Now 'foo' has been set to 'bar' on {$current->alias}!\n";
42

    
43
// Read operation.
44
$bar = $client->get('foo');
45
$current = $client->getConnection()->getCurrent()->getParameters();
46
echo "We just fetched 'foo' from {$current->alias} and its value is '$bar'.\n";
47

    
48
/* OUTPUT:
49
Does 'foo' exist on slave? yes.
50
Now 'foo' has been set to 'bar' on master!
51
We just fetched 'foo' from master and its value is 'bar'.
52
*/
(4-4/17)