MonitorContext.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // This is a basic example on how to use the Predis\MonitorContext class.
  12. // You can use redis-cli to send commands to the same Redis instance your client is
  13. // connected to, and then type "ECHO QUIT_MONITOR" in redis-cli when you want to
  14. // exit the monitor loop and terminate this script in a graceful way.
  15. // Create a client and disable r/w timeout on the socket.
  16. $client = new Predis\Client($single_server + array('read_write_timeout' => 0));
  17. // Use only one instance of DateTime, we will update the timestamp later.
  18. $timestamp = new DateTime();
  19. foreach (($monitor = $client->monitor()) as $event) {
  20. $timestamp->setTimestamp((int) $event->timestamp);
  21. // If we notice a ECHO command with the message QUIT_MONITOR, we close the
  22. // monitor context and then break the loop.
  23. if ($event->command === 'ECHO' && $event->arguments === '"QUIT_MONITOR"') {
  24. echo "Exiting the monitor loop...\n";
  25. $monitor->closeContext();
  26. break;
  27. }
  28. echo "* Received {$event->command} on DB {$event->database} at {$timestamp->format(DateTime::W3C)}\n";
  29. if (isset($event->arguments)) {
  30. echo " Arguments: {$event->arguments}\n";
  31. }
  32. }
  33. // Say goodbye :-)
  34. $info = $client->info();
  35. print_r("Goodbye from Redis v{$info['redis_version']}!\n");