Added RedisDebug for profiling redis parts
This commit is contained in:
99
redis/RedisManager.php
Normal file
99
redis/RedisManager.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Redis
|
||||
* @since 2010-07-17
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
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)
|
||||
{
|
||||
if (!isset(self::$connections[$name])) {
|
||||
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);
|
||||
}
|
||||
if ($database) {
|
||||
if (!$connection->select($database)) {
|
||||
throw new Exception('Failed to select Redis database with index ' . $database);
|
||||
}
|
||||
}
|
||||
self::$connections[$name] = $connection;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user