Skip to content

Commit fa9faab

Browse files
committed
add caches to validator's DocumentUtils
1 parent 176cbff commit fa9faab

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Validation/DocumentUtils.php

+31
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
class DocumentUtils
1111
{
12+
private static $cacheNodesOfKey = [];
13+
private static $cacheNodesOfKind = [];
14+
1215
/**
1316
* Returns all FragmentDefinitions
1417
* @param array $document
@@ -29,6 +32,10 @@ public static function getFragmentDefinitions(array $document): array
2932
*/
3033
public static function getAllNodesOfKind(array $document, string $kind): array
3134
{
35+
$hashKey = self::getHashKey($document, $kind);
36+
if ((self::$cacheNodesOfKind[$hashKey] ?? null) !== null)
37+
return self::$cacheNodesOfKind[$hashKey];
38+
3239
$keys = array_keys($document);
3340

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

52+
self::$cacheNodesOfKind[$hashKey] = $nodes;
53+
4554
return $nodes;
4655
}
4756

@@ -53,6 +62,10 @@ public static function getAllNodesOfKind(array $document, string $kind): array
5362
*/
5463
public static function getAllNodesOfKey(array $document, string $key): array
5564
{
65+
$hashKey = self::getHashKey($document, $key);
66+
if ((self::$cacheNodesOfKey[$hashKey] ?? null) !== null)
67+
return self::$cacheNodesOfKey[$hashKey];
68+
5669
$keys = array_keys($document);
5770

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

82+
self::$cacheNodesOfKey[$hashKey] = $nodes;
83+
6984
return $nodes;
7085
}
86+
87+
/**
88+
* Builds an unique hash-key based on a document and an identifier.
89+
*
90+
* @param array $document
91+
* @param string $identifier
92+
* @return string
93+
*/
94+
private static function getHashKey(array $document, string $identifier): string
95+
{
96+
return crc32(
97+
serialize(
98+
$document
99+
)
100+
) . $identifier;
101+
}
71102
}
72103

0 commit comments

Comments
 (0)