Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(Framework): Add Record of execute sql and redis key hit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Feb 7, 2019
1 parent ffca1e7 commit 4799813
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
11 changes: 11 additions & 0 deletions framework/Database/BasePDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class BasePDOConnection extends Component
// sql原始数据
protected $_sqlPrepareData = [];

protected $_sqlExecuteData = [];

// 默认驱动连接选项
protected $_defaultDriverOptions = [
\PDO::ATTR_EMULATE_PREPARES => false,
Expand All @@ -63,6 +65,11 @@ public function onInitialize()
$this->_driverOptions = $this->driverOptions + $this->_defaultDriverOptions;
}

public function onRequestAfter()
{
$this->_sqlExecuteData = [];
}

// 创建连接
protected function createConnection()
{
Expand Down Expand Up @@ -196,6 +203,7 @@ protected function prepare()
// 清扫预处理数据
protected function clearPrepare()
{
$this->_sqlExecuteData[] = $this->getRawSql();
$this->_sql = '';
$this->_params = [];
$this->_values = [];
Expand Down Expand Up @@ -418,4 +426,7 @@ public function getRawSql()
return array_shift($sqlPrepareData);
}

public function getExecuteData() {
return $this->_sqlExecuteData;
}
}
27 changes: 23 additions & 4 deletions framework/Redis/BaseRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ class BaseRedisConnection extends Component
/** @var \Redis */
protected $_redis;

protected $_calledData = [];

// 初始化事件
public function onInitialize()
{
Expand All @@ -218,6 +220,11 @@ public function onInitialize()
$this->_driverOptions = $this->driverOptions + $this->_defaultDriverOptions;
}

public function onRequestAfter()
{
$this->_calledData = [];
}

// 创建连接
protected function createConnection()
{
Expand Down Expand Up @@ -259,10 +266,17 @@ protected function autoConnect()
// 执行命令
public function __call($name, $arguments)
{
// 自动连接
$this->autoConnect();
// 执行命令
return call_user_func_array([$this->_redis, $name], $arguments);

$this->autoConnect(); // 自动连接

$calling = $name . ($arguments ? ' ' . $arguments[0] : '');
if (isset($this->_calledData[$calling])) {
$this->_calledData[$calling] += 1;
} else {
$this->_calledData[$calling] = 1;
}

return call_user_func_array([$this->_redis, $name], $arguments); // 执行命令
}

// 扩展方法
Expand All @@ -284,4 +298,9 @@ public function typeof($key): ?string
return "Not Found";
}
}

public function getCalledData()
{
return $this->_calledData;
}
}

0 comments on commit 4799813

Please sign in to comment.