This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
48 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
/** | ||
* Redis service class | ||
* Abstract Service class | ||
* | ||
* PHP version 5 | ||
* | ||
|
@@ -11,56 +11,67 @@ | |
* @copyright Copyright 2012, Wan Qi Chen <[email protected]> | ||
* @link http://resqueboard.kamisama.me | ||
* @package resqueboard | ||
* @subpackage resqueboard.lib | ||
* @subpackage resqueboard.lib.service | ||
* @since 1.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
|
||
namespace ResqueBoard\Lib\Service; | ||
|
||
/** | ||
* Redis service class | ||
* Abstract Service class | ||
* | ||
* @subpackage resqueboard.lib.service | ||
* @since 1.0.0 | ||
* Implements function to log all services activities | ||
* | ||
* @subpackage resqueboard.lib.service | ||
* @since 2.0.0 | ||
* @author Wan Qi Chen <[email protected]> | ||
*/ | ||
trait Loggable | ||
abstract class AbstractService | ||
{ | ||
protected static $_logs = array(); | ||
|
||
protected static $_totalTime = 0; | ||
protected static $_totalTime = array(); | ||
|
||
protected static $_totalQueries = 0; | ||
protected static $_totalQueries = array(); | ||
|
||
protected static $_maxLogs = 200; | ||
|
||
public static $serviceInstance = array(); | ||
|
||
public function __call($name, $args) | ||
{ | ||
$t = microtime(true); | ||
$result = call_user_func_array(array(self::$serviceInstance, $name), $args); | ||
$result = call_user_func_array(array(self::$serviceInstance[get_called_class()], $name), $args); | ||
$queryTime = round((microtime(true) - $t) * 1000, 2); | ||
self::logQuery( | ||
array( | ||
'command' => strtoupper($name) . ' ' . multiImplode(' ', $args), | ||
'time' => $queryTime | ||
) | ||
); | ||
self::$_totalTime += $queryTime; | ||
self::$_totalQueries++; | ||
self::$_totalTime[get_called_class()] += $queryTime; | ||
self::$_totalQueries[get_called_class()]++; | ||
return $result; | ||
} | ||
|
||
protected function bootstrap() | ||
{ | ||
self::$_totalTime[get_called_class()] = 0; | ||
self::$_totalQueries[get_called_class()] = 0; | ||
self::$_logs[get_called_class()] = array(); | ||
} | ||
|
||
protected static function logQuery($log) | ||
{ | ||
$trace = debug_backtrace(); | ||
|
||
$log['trace']['file'] = $trace[1]['file']; | ||
$log['trace']['line'] = $trace[1]['line']; | ||
|
||
self::$_logs[] = $log; | ||
if (count(self::$_logs) > self::$_maxLogs) { | ||
array_shift(self::$_logs); | ||
self::$_logs[get_called_class()][] = $log; | ||
if (count(self::$_logs[get_called_class()]) > self::$_maxLogs) { | ||
array_shift(self::$_logs[get_called_class()]); | ||
} | ||
} | ||
|
||
|
@@ -72,13 +83,11 @@ protected static function logQuery($log) | |
public static function getLogs() | ||
{ | ||
return array( | ||
'count' => self::$_totalQueries, | ||
'time' => self::$_totalTime, | ||
'logs' => self::$_logs | ||
'count' => self::$_totalQueries[get_called_class()], | ||
'time' => self::$_totalTime[get_called_class()], | ||
'logs' => self::$_logs[get_called_class()] | ||
); | ||
} | ||
|
||
|
||
} | ||
|
||
function multiImplode($glue, $pieces) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,20 +25,18 @@ | |
* @since 1.0.0 | ||
* @author Wan Qi Chen <[email protected]> | ||
*/ | ||
class Cube | ||
class Cube extends AbstractService | ||
{ | ||
use Loggable; | ||
|
||
public static $instance = null; | ||
|
||
public static $serviceInstance = null; | ||
|
||
protected $settings = array(); | ||
|
||
public function __construct($settings) | ||
{ | ||
parent::bootstrap(); | ||
|
||
$this->settings = $settings; | ||
self::$serviceInstance = $this; | ||
parent::$serviceInstance[get_class()] = $this; | ||
} | ||
|
||
public static function init($settings) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,28 +25,26 @@ | |
* @since 1.0.0 | ||
* @author Wan Qi Chen <[email protected]> | ||
*/ | ||
class Mongo | ||
class Mongo extends AbstractService | ||
{ | ||
use Loggable; | ||
|
||
public static $instance = null; | ||
|
||
public static $serviceInstance = null; | ||
|
||
public function __construct($settings) | ||
{ | ||
parent::bootstrap(); | ||
|
||
$t = microtime(true); | ||
self::$serviceInstance = new \Mongo($settings['host'] . ':' . $settings['port']); | ||
parent::$serviceInstance[get_class()] = new \Mongo($settings['host'] . ':' . $settings['port']); | ||
|
||
$queryTime = round((microtime(true) - $t) * 1000, 2); | ||
self::logQuery( | ||
parent::logQuery( | ||
array( | ||
'command' => 'CONNECTION to ' . $settings['host'] . ':' . $settings['port'], | ||
'time' => $queryTime | ||
) | ||
); | ||
self::$_totalTime += $queryTime; | ||
self::$_totalQueries++; | ||
parent::$_totalTime[get_class()] += $queryTime; | ||
parent::$_totalQueries[get_class()]++; | ||
} | ||
|
||
public static function init($settings) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,16 +25,14 @@ | |
* @since 1.0.0 | ||
* @author Wan Qi Chen <[email protected]> | ||
*/ | ||
class Redis | ||
class Redis extends AbstractService | ||
{ | ||
use Loggable; | ||
|
||
public static $instance = null; | ||
|
||
public static $serviceInstance = null; | ||
|
||
public function __construct($settings) | ||
{ | ||
parent::bootstrap(); | ||
|
||
$t = microtime(true); | ||
$redis = new \Redis(); | ||
$redis->connect($settings['host'], $settings['port']); | ||
|
@@ -43,7 +41,7 @@ public function __construct($settings) | |
$redis->setOption(\Redis::OPT_PREFIX, $settings['prefix'] . ':'); | ||
} | ||
|
||
self::$serviceInstance = $redis; | ||
parent::$serviceInstance[get_class()] = $redis; | ||
|
||
$queryTime = round((microtime(true) - $t) * 1000, 2); | ||
self::logQuery( | ||
|
@@ -52,8 +50,8 @@ public function __construct($settings) | |
'time' => $queryTime | ||
) | ||
); | ||
self::$_totalTime += $queryTime; | ||
self::$_totalQueries++; | ||
self::$_totalTime[get_class()] += $queryTime; | ||
self::$_totalQueries[get_class()]++; | ||
} | ||
|
||
public static function init($settings) | ||
|
@@ -68,7 +66,7 @@ public static function init($settings) | |
public function pipeline($commands, $type = \Redis::PIPELINE) | ||
{ | ||
$t = microtime(true); | ||
$redisPipeline = self::$serviceInstance->multi($type); | ||
$redisPipeline = parent::$serviceInstance[get_class()]->multi($type); | ||
foreach ($commands as $command) { | ||
call_user_func_array(array($redisPipeline, $command[0]), (array)$command[1]); | ||
} | ||
|
@@ -80,8 +78,8 @@ public function pipeline($commands, $type = \Redis::PIPELINE) | |
'time' => $queryTime | ||
) | ||
); | ||
self::$_totalTime += $queryTime; | ||
self::$_totalQueries++; | ||
self::$_totalTime[get_class()] += $queryTime; | ||
self::$_totalQueries[get_class()]++; | ||
|
||
return $results; | ||
} | ||
|