MasterSlaveReplicationComplex.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require 'SharedConfigurations.php';
  11. // Predis allows to set Lua scripts as read-only operations for replication.
  12. // This works for both EVAL and EVALSHA and also for the client-side abstraction
  13. // built upon them (Predis\Command\ScriptCommand). This example shows a slightly
  14. // more complex configuration that injects a new script command in the server
  15. // profile used by the new client instance and marks it marks it as a read-only
  16. // operation for replication so that it will be executed on slaves.
  17. use Predis\Command\ScriptCommand;
  18. use Predis\Connection\MasterSlaveReplication;
  19. use Predis\Replication\ReplicationStrategy;
  20. // ------------------------------------------------------------------------- //
  21. // Define a new script command that returns all the fields of a variable number
  22. // of hashes with a single roundtrip.
  23. class HashMultipleGetAll extends ScriptCommand {
  24. const BODY = <<<EOS
  25. local hashes = {}
  26. for _, key in pairs(KEYS) do
  27. table.insert(hashes, key)
  28. table.insert(hashes, redis.call('hgetall', key))
  29. end
  30. return hashes
  31. EOS;
  32. public function getScript() {
  33. return self::BODY;
  34. }
  35. }
  36. // ------------------------------------------------------------------------- //
  37. $parameters = array(
  38. 'tcp://127.0.0.1:6379/?alias=master',
  39. 'tcp://127.0.0.1:6380/?alias=slave',
  40. );
  41. $options = array(
  42. 'profile' => function ($options, $option) {
  43. $profile = $options->getDefault($option);
  44. $profile->defineCommand('hmgetall', 'HashMultipleGetAll');
  45. return $profile;
  46. },
  47. 'replication' => function ($options) {
  48. $strategy = new ReplicationStrategy();
  49. $strategy->setScriptReadOnly(HashMultipleGetAll::BODY);
  50. $replication = new MasterSlaveReplication($strategy);
  51. return $replication;
  52. },
  53. );
  54. // ------------------------------------------------------------------------- //
  55. $client = new Predis\Client($parameters, $options);
  56. // Execute the following commands on the master server using redis-cli:
  57. // $ ./redis-cli HMSET metavars foo bar hoge piyo
  58. // $ ./redis-cli HMSET servers master host1 slave host2
  59. $hashes = $client->hmgetall('metavars', 'servers');
  60. $replication = $client->getConnection();
  61. $stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave');
  62. echo "Is still on slave? ", $stillOnSlave ? 'YES' : 'NO', "!\n";
  63. var_export($hashes);