Skip to content

Commit

Permalink
Fixed and optimized CachedContentTypeLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Dunogier committed Aug 31, 2019
1 parent b211121 commit 75afdd7
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/GraphQL/DataLoader/CachedContentTypeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class CachedContentTypeLoader implements ContentTypeLoader
*/
private $loadedItems = [];

/**
* @var array
*/
private $identifierToIdMap = [];

/**
* @var ContentType[]
*/
private $loadedItemsByIdentifier = [];

public function __construct(ContentTypeLoader $innerLoader)
{
$this->innerLoader = $innerLoader;
Expand All @@ -31,20 +41,23 @@ public function __construct(ContentTypeLoader $innerLoader)
public function load($contentTypeId): ContentType
{
if (!isset($this->loadedItems[$contentTypeId])) {
$this->loadedItems[$contentTypeId] = $this->innerLoader->load($contentTypeId);
$contentType = $this->innerLoader->load($contentTypeId);
$this->loadedItems[$contentTypeId] = $contentType;
$this->identifierToIdMap[$contentType->identifier] = $contentTypeId;
}

return $this->loadedItems[$contentTypeId];
}

public function loadByIdentifier($identifier): ContentType
{
$contentType = $this->innerLoader->loadByIdentifier($identifier);

if (!isset($this->innerLoader[$contentType->id])) {
$this->innerLoader[$contentType->id] = $contentType;
if (!isset($this->identifierToIdMap[$identifier])) {
$contentType = $this->innerLoader->loadByIdentifier($identifier);
$this->loadedItems[$contentType->id] = $contentType;
$this->identifierToIdMap[$identifier] = $contentType->id;
}

return $contentType;

return $this->loadedItems[$this->identifierToIdMap[$identifier]];
}
}

0 comments on commit 75afdd7

Please sign in to comment.