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 allows to set Lua scripts as read-only operations in the context of
|
15
|
// replication. This works for both EVAL and EVALSHA and also for the client-side
|
16
|
// abstraction built upon them (Predis\Command\ScriptedCommand). This example
|
17
|
// shows a slightly more complex configuration that injects a new scripted command
|
18
|
// in the server profile used by the new client instance and marks it marks it as
|
19
|
// a read-only operation for replication so that it will be executed on slaves.
|
20
|
|
21
|
use Predis\Command\ScriptedCommand;
|
22
|
use Predis\Connection\MasterSlaveReplication;
|
23
|
use Predis\Profile\ServerProfile;
|
24
|
use Predis\Replication\ReplicationStrategy;
|
25
|
|
26
|
// ------------------------------------------------------------------------- //
|
27
|
|
28
|
// Define a new scripted command that returns all the fields
|
29
|
// of a variable number of hashes with a single roundtrip.
|
30
|
|
31
|
class HashMultipleGetAll extends ScriptedCommand {
|
32
|
const BODY = <<<EOS
|
33
|
local hashes = {}
|
34
|
for _, key in pairs(KEYS) do
|
35
|
table.insert(hashes, key)
|
36
|
table.insert(hashes, redis.call('hgetall', key))
|
37
|
end
|
38
|
return hashes
|
39
|
EOS;
|
40
|
|
41
|
public function getScript() {
|
42
|
return self::BODY;
|
43
|
}
|
44
|
}
|
45
|
|
46
|
// ------------------------------------------------------------------------- //
|
47
|
|
48
|
$parameters = array(
|
49
|
'tcp://127.0.0.1:6379/?alias=master',
|
50
|
'tcp://127.0.0.1:6380/?alias=slave',
|
51
|
);
|
52
|
|
53
|
$options = array(
|
54
|
'profile' => function ($options, $option) {
|
55
|
$profile = $options->getDefault($option);
|
56
|
$profile->defineCommand('hmgetall', 'HashMultipleGetAll');
|
57
|
|
58
|
return $profile;
|
59
|
},
|
60
|
'replication' => function ($options) {
|
61
|
$strategy = new ReplicationStrategy();
|
62
|
$strategy->setScriptReadOnly(HashMultipleGetAll::BODY);
|
63
|
|
64
|
$replication = new MasterSlaveReplication($strategy);
|
65
|
|
66
|
return $replication;
|
67
|
},
|
68
|
);
|
69
|
|
70
|
// ------------------------------------------------------------------------- //
|
71
|
|
72
|
$client = new Predis\Client($parameters, $options);
|
73
|
|
74
|
// Execute the following commands on the master server using redis-cli:
|
75
|
// $ ./redis-cli HMSET metavars foo bar hoge piyo
|
76
|
// $ ./redis-cli HMSET servers master host1 slave host2
|
77
|
|
78
|
$hashes = $client->hmgetall('metavars', 'servers');
|
79
|
|
80
|
$replication = $client->getConnection();
|
81
|
$stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave');
|
82
|
|
83
|
echo "Is still on slave? ", $stillOnSlave ? 'YES' : 'NO', "!\n";
|
84
|
var_export($hashes);
|