Skip to content

Commit

Permalink
同步性能优化
Browse files Browse the repository at this point in the history
  • Loading branch information
dogstarTest committed Aug 5, 2017
1 parent eed0787 commit 72a80d1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
7 changes: 4 additions & 3 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,11 @@ protected function userCheck() {
* @return boolean
*/
protected function isServiceWhitelist() {
$api = DI()->request->getServiceApi();
$action = DI()->request->getServiceAction();
$di = DI();
$api = $di->request->getServiceApi();
$action = $di->request->getServiceAction();

$serviceWhitelist = DI()->config->get('app.service_whitelist', array());
$serviceWhitelist = $di->config->get('app.service_whitelist', array());
foreach ($serviceWhitelist as $item) {
$cfgArr = explode('.', $item);
if (count($cfgArr) < 2) {
Expand Down
9 changes: 5 additions & 4 deletions src/ApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ class ApiFactory {
* @throws BadRequestException 非法请求下返回400
*/
static function generateService($isInitialize = TRUE) {
$service = DI()->request->getService();
$namespace = DI()->request->getNamespace();
$api = DI()->request->getServiceApi();
$action = DI()->request->getServiceAction();
$di = DI();
$service = $di->request->getService();
$namespace = $di->request->getNamespace();
$api = $di->request->getServiceApi();
$action = $di->request->getServiceAction();

if (empty($api) || empty($action)) {
throw new BadRequestException(
Expand Down
26 changes: 7 additions & 19 deletions src/DependenceInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function onInitialize() {
* @parms mixed $value service的值,可以是具体的值或实例、类名、匿名函数、数组配置
*/
public function set($key, $value) {
$this->resetHit($key);
$this->hitTimes[$key] = 0;

$this->data[$key] = $value;

Expand All @@ -137,9 +137,13 @@ public function get($key, $default = NULL) {
$this->data[$key] = $default;
}

$this->recordHitTimes($key);
// 内联操作,减少函数调用,提升性能
if (!isset($this->hitTimes[$key])) {
$this->hitTimes[$key] = 0;
}
$this->hitTimes[$key] ++;

if ($this->isFirstHit($key)) {
if ($this->hitTimes[$key] == 1) {
$this->data[$key] = $this->initService($this->data[$key]);
}

Expand Down Expand Up @@ -206,21 +210,5 @@ protected function initService($config) {

return $rs;
}

protected function resetHit($key) {
$this->hitTimes[$key] = 0;
}

protected function isFirstHit($key) {
return $this->hitTimes[$key] == 1;
}

protected function recordHitTimes($key) {
if (!isset($this->hitTimes[$key])) {
$this->hitTimes[$key] = 0;
}

$this->hitTimes[$key] ++;
}
}

13 changes: 7 additions & 6 deletions src/PhalApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class PhalApi {
```
*/
public function response() {
$rs = DI()->response;
$di = DI();
$rs = $di->response;
try {
// 接口调度与响应
$api = ApiFactory::generateService();
$action = DI()->request->getServiceAction();
$action = $di->request->getServiceAction();
$data = call_user_func(array($api, $action));

$rs->setData($data);
Expand All @@ -54,9 +55,9 @@ public function response() {
$rs->setMsg($ex->getMessage());
} catch (\Exception $ex) {
// 不可控的异常
DI()->logger->error(DI()->request->getService(), strval($ex));
$di->logger->error(DI()->request->getService(), strval($ex));

if (DI()->debug) {
if ($di->debug) {
$rs->setRet($ex->getCode());
$rs->setMsg($ex->getMessage());
$rs->setDebug('exception', $ex->getTrace());
Expand All @@ -65,8 +66,8 @@ public function response() {
}
}

$rs->setDebug('stack', DI()->tracer->getStack());
$rs->setDebug('sqls', DI()->tracer->getSqls());
$rs->setDebug('stack', $di->tracer->getStack());
$rs->setDebug('sqls', $di->tracer->getSqls());
$rs->setDebug('version', PHALAPI_VERSION);

return $rs;
Expand Down
11 changes: 9 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Request {
/**
* @var array $headers 备用数据源 请求头部信息
*/
protected $headers = array();
protected $headers;

/**
* @var string 接口服务命名空间
Expand Down Expand Up @@ -91,7 +91,6 @@ public function __construct($data = NULL) {
$this->data = $this->genData($data);

// 备用数据源
$this->headers = $this->getAllHeaders();
$this->get = $_GET;
$this->post = $_POST;
$this->request = $_REQUEST;
Expand Down Expand Up @@ -150,6 +149,11 @@ protected function getAllHeaders() {
* @return string
*/
public function getHeader($key, $default = NULL) {
// 延时加载,提升性能
if ($this->headers === NULL) {
$this->headers = $this->getAllHeaders();
}

return isset($this->headers[$key]) ? $this->headers[$key] : $default;
}

Expand Down Expand Up @@ -223,6 +227,9 @@ protected function &getDataBySource($source) {
case 'COOKIE':
return $this->cookie;
case 'HEADER':
if ($this->headers === NULL) {
$this->headers = $this->getAllHeaders();
}
return $this->headers;
case 'SERVER':
return $_SERVER;
Expand Down

0 comments on commit 72a80d1

Please sign in to comment.