123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- require 'SharedConfigurations.php';
- use Predis\Profiles\ServerProfile;
- use Predis\Commands\ScriptedCommand;
- use Predis\Network\MasterSlaveReplication;
- class HashMultipleGetAll extends ScriptedCommand {
- const BODY = <<<EOS
- local hashes = {}
- for _, key in pairs(KEYS) do
- table.insert(hashes, key)
- table.insert(hashes, redis.call('hgetall', key))
- end
- return hashes
- EOS;
- public function getScript() {
- return self::BODY;
- }
- }
- $parameters = array(
- 'tcp://127.0.0.1:6379/?alias=master',
- 'tcp://127.0.0.1:6380/?alias=slave',
- );
- $options = array(
- 'profile' => function($options) {
- $profile = ServerProfile::get('2.6');
- $profile->defineCommand('hmgetall', 'HashMultipleGetAll');
- return $profile;
- },
- 'replication' => function($options) {
- $replication = new MasterSlaveReplication();
- $replication->setScriptReadOnly(HashMultipleGetAll::BODY);
- return $replication;
- },
- );
- $client = new Predis\Client($parameters, $options);
- $hashes = $client->hmgetall('metavars', 'servers');
- $replication = $client->getConnection();
- $stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave');
- echo "Is still on slave? ", $stillOnSlave ? 'YES' : 'NO', "!\n";
- var_export($hashes);
|