Skip to content

Commit 24160f1

Browse files
author
Анатолий Нехай
committed
in-memory caching of route regexes
1 parent 86a2b59 commit 24160f1

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

functions/path_regex.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
*/
2727
function path_regex(string $path) : string
2828
{
29+
// This will be useful for a long-running application,
30+
// for example if you use the RoadRunner server...
31+
static $cache = [];
32+
33+
if (isset($cache[$path])) {
34+
return $cache[$path];
35+
}
36+
2937
$matches = path_parse($path);
3038

3139
foreach ($matches as $match) {
@@ -42,5 +50,7 @@ function path_regex(string $path) : string
4250
$path = str_replace('{' . $match['name'] . '}', $subpattern, $path);
4351
}
4452

45-
return '#^' . $path . '$#uD';
53+
$cache[$path] = '#^' . $path . '$#uD';
54+
55+
return $cache[$path];
4656
}

0 commit comments

Comments
 (0)