MasterSlaveReplicationComplex.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Profile\ServerProfile;
  20. use Predis\Replication\ReplicationStrategy;
  21. // ------------------------------------------------------------------------- //
  22. // Define a new scripted command that returns all the fields
  23. // of a variable number of hashes with a single roundtrip.
  24. class HashMultipleGetAll extends ScriptedCommand {
  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. return self::BODY;
  35. }
  36. }
  37. // ------------------------------------------------------------------------- //
  38. $parameters = array(
  39. 'tcp://127.0.0.1:6379/?alias=master',
  40. 'tcp://127.0.0.1:6380/?alias=slave',
  41. );
  42. $options = array(
  43. 'profile' => function ($options) {
  44. $profile = ServerProfile::get('2.6');
  45. $profile->defineCommand('hmgetall', 'HashMultipleGetAll');
  46. return $profile;
  47. },
  48. 'replication' => function ($options) {
  49. $strategy = new ReplicationStrategy();
  50. $strategy->setScriptReadOnly(HashMultipleGetAll::BODY);
  51. $replication = new MasterSlaveReplication($strategy);
  52. return $replication;
  53. },
  54. );
  55. // ------------------------------------------------------------------------- //
  56. $client = new Predis\Client($parameters, $options);
  57. // Execute the following commands on the master server using redis-cli:
  58. // $ ./redis-cli HMSET metavars foo bar hoge piyo
  59. // $ ./redis-cli HMSET servers master host1 slave host2
  60. $hashes = $client->hmgetall('metavars', 'servers');
  61. $replication = $client->getConnection();
  62. $stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave');
  63. echo "Is still on slave? ", $stillOnSlave ? 'YES' : 'NO', "!\n";
  64. var_export($hashes);