SimpleDebuggableConnection.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. require 'SharedConfigurations.php';
  3. use Predis\ConnectionParameters;
  4. use Predis\Commands\ICommand;
  5. use Predis\Network\StreamConnection;
  6. class SimpleDebuggableConnection extends StreamConnection
  7. {
  8. private $_tstart = 0;
  9. private $_debugBuffer = array();
  10. public function connect()
  11. {
  12. $this->_tstart = microtime(true);
  13. parent::connect();
  14. }
  15. private function storeDebug(ICommand $command, $direction)
  16. {
  17. $firtsArg = $command->getArgument(0);
  18. $timestamp = round(microtime(true) - $this->_tstart, 4);
  19. $debug = $command->getId();
  20. $debug .= isset($firtsArg) ? " $firtsArg " : ' ';
  21. $debug .= "$direction $this";
  22. $debug .= " [{$timestamp}s]";
  23. $this->_debugBuffer[] = $debug;
  24. }
  25. public function writeCommand(ICommand $command)
  26. {
  27. parent::writeCommand($command);
  28. $this->storeDebug($command, '->');
  29. }
  30. public function readResponse(ICommand $command)
  31. {
  32. $reply = parent::readResponse($command);
  33. $this->storeDebug($command, '<-');
  34. return $reply;
  35. }
  36. public function getDebugBuffer()
  37. {
  38. return $this->_debugBuffer;
  39. }
  40. }
  41. $options = array(
  42. 'connections' => array(
  43. 'tcp' => 'SimpleDebuggableConnection',
  44. ),
  45. );
  46. $redis = new Predis\Client($single_server, $options);
  47. $redis->set('foo', 'bar');
  48. $redis->get('foo');
  49. $redis->info();
  50. print_r($redis->getConnection()->getDebugBuffer());
  51. /* OUTPUT:
  52. Array
  53. (
  54. [0] => SELECT 15 -> 127.0.0.1:6379 [0.0008s]
  55. [1] => SELECT 15 <- 127.0.0.1:6379 [0.0012s]
  56. [2] => SET foo -> 127.0.0.1:6379 [0.0014s]
  57. [3] => SET foo <- 127.0.0.1:6379 [0.0014s]
  58. [4] => GET foo -> 127.0.0.1:6379 [0.0016s]
  59. [5] => GET foo <- 127.0.0.1:6379 [0.0018s]
  60. [6] => INFO -> 127.0.0.1:6379 [0.002s]
  61. [7] => INFO <- 127.0.0.1:6379 [0.0025s]
  62. )
  63. */