This commit is contained in:
2021-02-23 17:43:13 +08:00
parent 29fc384741
commit 1c0cd04fcc
6 changed files with 37 additions and 14 deletions
+2 -2
View File
@@ -181,7 +181,7 @@ class ActiveQuery extends Component
*/
public function page(int $size, callable $callback): Pagination
{
$pagination = objectPool(Pagination::class, [$this]);
$pagination = new Pagination($this);
$pagination->setOffset(0);
$pagination->setLimit($size);
$pagination->setCallback($callback);
@@ -207,7 +207,7 @@ class ActiveQuery extends Component
public function all(): Collection|array
{
$data = $this->modelClass::getDb()->createCommand($this->queryBuilder())->all();
$collect = objectPool(Collection::class, [$this, $data, $this->modelClass]);
$collect = new Collection($this, $data, $this->modelClass);
if ($this->asArray) {
return $collect->toArray();
}
+12 -6
View File
@@ -315,11 +315,6 @@ class ActiveRecord extends BaseActiveRecord
*/
public function toArray(): array
{
$class = $this;
Coroutine::defer(function () use ($class) {
$object = Snowflake::app()->getObject();
$object->release(get_called_class(), $class);
});
$data = $this->_attributes;
foreach ($this->getAnnotation() as $key => $item) {
if (!isset($data[$key])) {
@@ -327,7 +322,18 @@ class ActiveRecord extends BaseActiveRecord
}
$data[$key] = call_user_func([$this, $item[1]], $data[$key]);
}
return array_merge($data, $this->runRelate());
$data = array_merge($data, $this->runRelate());
$this->recover();
return $data;
}
/**
* @throws ComponentException
*/
public function recover()
{
objectRecover(get_called_class(), $this);
}
+4 -2
View File
@@ -26,15 +26,17 @@ class Dispatch
protected mixed $request;
/**
* @param $handler
* @param $request
* @return static
* @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
*/
public static function create($handler, $request): static
{
$class = objectPool(get_called_class());
$class = new static();
$class->handler = $handler;
$class->request = $request;
if ($handler instanceof Closure) {
+4 -1
View File
@@ -115,7 +115,10 @@ class Kafka extends \Snowflake\Process\Process
if (!($class instanceof ConsumerInterface)) {
return;
}
$class->onHandler(objectPool(Struct::class, [$topic, $message]));
$class->onHandler($Struct = objectPool(Struct::class, [$topic, $message]));
objectRecover($namespace, $class);
objectRecover(Struct::class, $Struct);
} catch (Throwable $exception) {
$this->application->error($exception);
}
+1 -3
View File
@@ -38,9 +38,7 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
if (is_object($config)) {
return $config;
}
$object = $this->getFromChannel($name = md5($config), [$config, $construct]);
listen(Event::EVENT_AFTER_REQUEST, [$this, 'release'], [$name, $object]);
return $object;
return $this->getFromChannel($name = md5($config), [$config, $construct]);
}
+14
View File
@@ -233,6 +233,20 @@ if (!function_exists('objectPool')) {
}
}
if (!function_exists('objectRecover')) {
/**
* @param string $className
* @param $object
* @return mixed
* @throws ComponentException
*/
function objectRecover(mixed $className, $object): void
{
Snowflake::app()->getObject()->release($className, $object);
}
}
if (!function_exists('instance_load')) {