MasterSlaveReplicationComplex.php 2.6 KB

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