SimpleDebuggableConnection.php 1.8 KB

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