FireAndForgetExecutor.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. namespace Predis\Pipeline;
  11. use SplQueue;
  12. use Predis\Connection\ConnectionInterface;
  13. use Predis\Connection\ReplicationConnectionInterface;
  14. /**
  15. * Implements a pipeline executor strategy that writes a list of commands to
  16. * the connection object but does not read back their replies.
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class FireAndForgetExecutor implements PipelineExecutorInterface
  21. {
  22. /**
  23. * Allows the pipeline executor to perform operations on the
  24. * connection before starting to execute the commands stored
  25. * in the pipeline.
  26. *
  27. * @param ConnectionInterface Connection instance.
  28. */
  29. protected function checkConnection(ConnectionInterface $connection)
  30. {
  31. if ($connection instanceof ReplicationConnectionInterface) {
  32. $connection->switchTo('master');
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function execute(ConnectionInterface $connection, SplQueue $commands)
  39. {
  40. $this->checkConnection($connection);
  41. while (!$commands->isEmpty()) {
  42. $connection->writeCommand($commands->dequeue());
  43. }
  44. $connection->disconnect();
  45. return array();
  46. }
  47. }