SimpleDebuggableConnection.php 1.7 KB

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