mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 08:12:17 +01:00
Performance optimization for import
This commit is contained in:
committed by
Constantin Graf
parent
90480f3bb8
commit
0eef5ffcfa
@@ -30,6 +30,16 @@ class ImportDatabaseHelper
|
||||
*/
|
||||
private ?array $mapIdentifierToKey = null;
|
||||
|
||||
/**
|
||||
* @var array<string, TModel|null>|null
|
||||
*/
|
||||
private ?array $mapKeyToModel = null;
|
||||
|
||||
/**
|
||||
* @var array<string, TModel|null>|null
|
||||
*/
|
||||
private ?array $mapIdentifierToModel = null;
|
||||
|
||||
/**
|
||||
* @var array<string, string>
|
||||
*/
|
||||
@@ -148,6 +158,47 @@ class ImportDatabaseHelper
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TModel
|
||||
*/
|
||||
public function getModelById(string $id): ?Model
|
||||
{
|
||||
if ($this->mapKeyToModel === null) {
|
||||
$this->mapKeyToModel = [];
|
||||
}
|
||||
if (isset($this->mapKeyToModel[$id])) {
|
||||
return $this->mapKeyToModel[$id];
|
||||
}
|
||||
/** @var TModel|null $model */
|
||||
$model = $this->getModelInstance()->find($id);
|
||||
if ($model !== null) {
|
||||
$this->mapKeyToModel[$id] = $model;
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $identifierData
|
||||
* @return TModel|null
|
||||
*/
|
||||
public function getModel(array $identifierData): ?Model
|
||||
{
|
||||
if ($this->mapIdentifierToModel === null) {
|
||||
$this->mapIdentifierToModel = [];
|
||||
}
|
||||
$hash = $this->getHash($identifierData);
|
||||
if (isset($this->mapIdentifierToModel[$hash])) {
|
||||
return $this->mapIdentifierToModel[$hash];
|
||||
}
|
||||
$model = $this->getModelInstance()->where($identifierData)->first();
|
||||
if ($model !== null) {
|
||||
$this->mapIdentifierToModel[$hash] = $model;
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $identifierData
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user