MonitorContext.php 1.3 KB

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