Performance optimization for import

This commit is contained in:
Constantin Graf
2024-06-10 17:03:55 +02:00
committed by Constantin Graf
parent 90480f3bb8
commit 0eef5ffcfa
8 changed files with 478 additions and 3 deletions

View File

@@ -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
*