From 77a62925d8b10e24467e6357036295922d395cdf Mon Sep 17 00:00:00 2001 From: Anton Terekhov Date: Thu, 28 Jul 2011 18:38:28 +0400 Subject: [PATCH] Added RedisDebug for profiling redis parts --- redis/RedisDebug.php | 45 +++++++++++++++++++++++++ RedisManager.php => redis/RedisManager.php | 54 +++++++++++++++++++++++++----- 2 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 redis/RedisDebug.php rename RedisManager.php => redis/RedisManager.php (61%) diff --git a/redis/RedisDebug.php b/redis/RedisDebug.php new file mode 100644 index 0000000..02e4d53 --- /dev/null +++ b/redis/RedisDebug.php @@ -0,0 +1,45 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Redis + * @since 2011-07-29 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class RedisDebug +{ + private $redis; + + public function __construct($redis) + { + if (!is_a($redis, 'Redis')) { + throw new MJException(); + } + $this->redis = $redis; + } + + public function __call($name, $arguments) + { + $command = $name . '(' . $this->r_implode(', ', $arguments) . ')'; + $profiler = Profiler::getInstance()->profilerQuery($command); + $data = call_user_func_array(array($this->redis, $name), $arguments); + $profiler->end(); + return $data; + } + + protected function r_implode($glue, $pieces) + { + $retVal = array(); + foreach ($pieces as $r_pieces) { + if (is_array($r_pieces)) { + $retVal[] = '(' . $this->r_implode($glue, $r_pieces) . ')'; + } else { + $retVal[] = $r_pieces; + } + } + return implode($glue, $retVal); + } +} \ No newline at end of file diff --git a/RedisManager.php b/redis/RedisManager.php similarity index 61% rename from RedisManager.php rename to redis/RedisManager.php index 230651d..8341edb 100644 --- a/RedisManager.php +++ b/redis/RedisManager.php @@ -11,20 +11,20 @@ class RedisManager { - + /** * Redis connections - * + * * @var array */ static protected $connections = array(); - + /** * Connect to redis - * + * * @param string $name connection name. If not set 'default' will be used. * @param array $config Configuration array. - * + * * @return Redis */ static public function connect($name = 'default', $config = null) @@ -33,19 +33,22 @@ class RedisManager if (!$config) { $config = Config::get('Redis')->$name; } - + if (!is_array($config)) { throw new Exception('Connection parameters must be an array'); } - + $host = isset($config['host']) ? $config['host'] : 'localhost'; $port = isset($config['port']) ? $config['port'] : 6379; $database = isset($config['database']) ? $config['database'] : 0; - + /** * @var Redis */ $connection = new Redis(); + if (defined('DEBUG') && DEBUG == true) { + $connection = new RedisDebug($connection); + } if (!$connection->connect($host, $port)) { throw new Exception('Failed to connect to Redis server at ' . $host . ':' . $port); } @@ -58,4 +61,39 @@ class RedisManager } return self::$connections[$name]; } +} + +class RedisDebug +{ + private $redis; + + public function __construct($redis) + { + if (!is_a($redis, 'Redis')) { + throw new MJException(); + } + $this->redis = $redis; + } + + public function __call($name, $arguments) + { + $command = $name . '(' . $this->r_implode(', ', $arguments) . ')'; + $profiler = Profiler::getInstance()->profilerQuery($command); + $data = call_user_func_array(array($this->redis, $name), $arguments); + $profiler->end(); + return $data; + } + + protected function r_implode($glue, $pieces) + { + $retVal = array(); + foreach ($pieces as $r_pieces) { + if (is_array($r_pieces)) { + $retVal[] = '(' . $this->r_implode($glue, $r_pieces) . ')'; + } else { + $retVal[] = $r_pieces; + } + } + return implode($glue, $retVal); + } } \ No newline at end of file