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

Commit

Permalink
perf(Process): Disable Pdo And Redis called data in custom process
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Aug 12, 2019
1 parent 358ba5d commit b744e81
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Feat
- **Auth:** Use JWT to set cookies content (bf897c6)
- **Auth/Login:** Add full Advanced Options support (6009dc8)
- **Secret:** Protect jwt key for env('APP_SECRET_KEY') (dfa67da)
- **ban_ips:** Store banned ip in components/Site (01084c9)

### Fix
Expand Down
2 changes: 1 addition & 1 deletion apps/models/form/Auth/UserLoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected function loadUserFromPdo()
/** @noinspection PhpUnused */
protected function isMaxUserSessionsReached()
{
$exist_session_count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `user_session_log` WHERE uid = :uid AND expired = -1')->bindParams([
$exist_session_count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `user_session_log` WHERE uid = :uid AND expired != 1')->bindParams([
'uid' => $this->self['id']
])->queryScalar();

Expand Down
7 changes: 3 additions & 4 deletions apps/process/TrackerAnnounceProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class TrackerAnnounceProcess extends Process
{
public function run()
{
do {
while (true) {
$data = app()->redis->brpoplpush(Constant::trackerToDealQueue, Constant::trackerBackupQueue, 5);
if ($data !== false) {
app()->pdo->beginTransaction();
try {
/* We got data from Http Server Like
/** We got data from Http Server Like
* [
* 'timestamp' => timestamp when controller receive the announce,
* 'queries' => $queries, 'role' => $role,
Expand All @@ -37,8 +37,7 @@ public function run()
// TODO deal with the items in backup_queue
}
}
\Rid::app()->cleanComponents();
} while ($data !== false);
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion framework/Base/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ protected function resetSleepTime()
final public function start($config)
{
$this->_config = $config;
$this->disablePdoAndRedisRecord();
$this->resetSleepTime();

println('New Custom process `' . static::class . '` added.');

while (true) {
$this->run();
\Rid::app()->cleanComponents();
sleep($this->getSleepTime());
}
}

private function disablePdoAndRedisRecord()
{
if (in_array('pdo', $this->_config['components'])) app()->pdo->setRecordData(false);
if (in_array('redis', $this->_config['components'])) app()->redis->setRecordData(false);
}
}
13 changes: 11 additions & 2 deletions framework/Database/BasePDOConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class BasePDOConnection extends Component
// sql原始数据
protected $_sqlPrepareData = [];

protected $_recordData = true;
protected $_sqlExecuteData = [];

// 默认驱动连接选项
Expand All @@ -66,7 +67,7 @@ public function onInitialize()

public function onRequestAfter()
{
$this->cleanSqlExecuteData();
$this->_recordData && $this->cleanSqlExecuteData();
}

// 创建连接
Expand Down Expand Up @@ -202,7 +203,7 @@ protected function prepare()
// 清扫预处理数据
protected function clearPrepare()
{
$this->_sqlExecuteData[] = $this->getRawSql();
if ($this->_recordData) $this->_sqlExecuteData[] = $this->getRawSql();
$this->_sql = '';
$this->_params = [];
$this->_values = [];
Expand Down Expand Up @@ -433,4 +434,12 @@ public function cleanSqlExecuteData()
public function getExecuteData() {
return $this->_sqlExecuteData;
}

/**
* @param bool $recordData
*/
public function setRecordData(bool $recordData): void
{
$this->_recordData = $recordData;
}
}
35 changes: 23 additions & 12 deletions framework/Redis/BaseRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class BaseRedisConnection extends Component
// 默认驱动连接选项
protected $_defaultDriverOptions = [
\Redis::OPT_SERIALIZER => \Redis::SERIALIZER_PHP, // 默认做序列化
\Redis::OPT_PREFIX => "",
\Redis::OPT_PREFIX => '',
];

// 驱动连接选项
Expand All @@ -211,6 +211,7 @@ class BaseRedisConnection extends Component
/** @var \Redis */
protected $_redis;

protected $_recordData = true;
protected $_calledData = [];

// 初始化事件
Expand Down Expand Up @@ -269,17 +270,19 @@ public function __call($name, $arguments)

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

$arg_text = '';
foreach ($arguments as $arg) {
if (!is_string($arg)) $arg = '[Array]';
$arg_text .= ' ' . $arg;
}
if ($this->_recordData) {
$arg_text = '';
foreach ($arguments as $arg) {
if (!is_string($arg)) $arg = '[Array]';
$arg_text .= ' ' . $arg;
}

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

return call_user_func_array([$this->_redis, $name], $arguments); // 执行命令
Expand Down Expand Up @@ -316,7 +319,15 @@ public function getCalledData()

public function cleanCalledData()
{
$this->_calledData = [];
$this->_recordData && $this->_calledData = [];
}

/**
* @param bool $recordData
*/
public function setRecordData(bool $recordData): void
{
$this->_recordData = $recordData;
}

/**
Expand Down

0 comments on commit b744e81

Please sign in to comment.