Skip to content

Commit

Permalink
优化单独使用orm的时候 可能的内存溢出问题
Browse files Browse the repository at this point in the history
setLog方法支持传入闭包对日志进行处理
废弃getDbLog、updateQueryTime、clearQueryTime及getQueryTimes方法
  • Loading branch information
liu21st committed Sep 16, 2023
1 parent b745eb5 commit 2d488af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
38 changes: 14 additions & 24 deletions src/DbManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
declare(strict_types=1);
declare (strict_types = 1);

namespace think;

use Closure;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
Expand Down Expand Up @@ -57,13 +58,6 @@ class DbManager
*/
protected $listen = [];

/**
* SQL日志.
*
* @var array
*/
protected $dbLog = [];

/**
* 查询次数.
*
Expand Down Expand Up @@ -159,11 +153,11 @@ public function setCache(CacheInterface $cache): void
/**
* 设置日志对象
*
* @param LoggerInterface $log 日志对象
* @param LoggerInterface|Closure $log 日志对象
*
* @return void
*/
public function setLog(LoggerInterface $log): void
public function setLog(LoggerInterface | Closure $log): void
{
$this->log = $log;
}
Expand All @@ -179,27 +173,24 @@ public function setLog(LoggerInterface $log): void
public function log(string $log, string $type = 'sql')
{
if ($this->log) {
$this->log->log($type, $log);
} else {
$this->dbLog[$type][] = $log;
if ($this->log instanceof Closure) {
call_user_func_array($this->log, [$type, $log]);
} else {
$this->log->log($type, $log);
}
}
}

/**
* 获得查询日志(没有设置日志对象使用).
*
* @deprecated
* @param bool $clear 是否清空
*
* @return array
*/
public function getDbLog(bool $clear = false): array
{
$logs = $this->dbLog;
if ($clear) {
$this->dbLog = [];
}

return $logs;
return [];
}

/**
Expand Down Expand Up @@ -314,17 +305,16 @@ public function raw(string $value): Raw

/**
* 更新查询次数.
*
* @deprecated
* @return void
*/
public function updateQueryTimes(): void
{
$this->queryTimes++;
}

/**
* 重置查询次数.
*
* @deprecated
* @return void
*/
public function clearQueryTimes(): void
Expand All @@ -334,7 +324,7 @@ public function clearQueryTimes(): void

/**
* 获得查询次数.
*
* @deprecated
* @return int
*/
public function getQueryTimes(): int
Expand Down
2 changes: 1 addition & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ protected function setUpdateWhere($where): void
* 设置当前模型的数据库连接.
*
* @param string $connection 数据表连接标识
*
*
* @return $this
*/
public function setConnection(string $connection)
Expand Down

0 comments on commit 2d488af

Please sign in to comment.