Skip to content

Commit

Permalink
add caches to validator's DocumentUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
joonlabs committed May 22, 2021
1 parent 176cbff commit fa9faab
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Validation/DocumentUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
class DocumentUtils
{
private static $cacheNodesOfKey = [];
private static $cacheNodesOfKind = [];

/**
* Returns all FragmentDefinitions
* @param array $document
Expand All @@ -29,6 +32,10 @@ public static function getFragmentDefinitions(array $document): array
*/
public static function getAllNodesOfKind(array $document, string $kind): array
{
$hashKey = self::getHashKey($document, $kind);
if ((self::$cacheNodesOfKind[$hashKey] ?? null) !== null)
return self::$cacheNodesOfKind[$hashKey];

$keys = array_keys($document);

$nodes = [];
Expand All @@ -42,6 +49,8 @@ public static function getAllNodesOfKind(array $document, string $kind): array
}
}

self::$cacheNodesOfKind[$hashKey] = $nodes;

return $nodes;
}

Expand All @@ -53,6 +62,10 @@ public static function getAllNodesOfKind(array $document, string $kind): array
*/
public static function getAllNodesOfKey(array $document, string $key): array
{
$hashKey = self::getHashKey($document, $key);
if ((self::$cacheNodesOfKey[$hashKey] ?? null) !== null)
return self::$cacheNodesOfKey[$hashKey];

$keys = array_keys($document);

$nodes = [];
Expand All @@ -66,7 +79,25 @@ public static function getAllNodesOfKey(array $document, string $key): array
}
}

self::$cacheNodesOfKey[$hashKey] = $nodes;

return $nodes;
}

/**
* Builds an unique hash-key based on a document and an identifier.
*
* @param array $document
* @param string $identifier
* @return string
*/
private static function getHashKey(array $document, string $identifier): string
{
return crc32(
serialize(
$document
)
) . $identifier;
}
}

0 comments on commit fa9faab

Please sign in to comment.