Skip to content

Commit 337bf12

Browse files
committed
-
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 253519a commit 337bf12

8 files changed

Lines changed: 28 additions & 36 deletions

File tree

bin/naive-benchmark.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,21 @@ private static function getImplementations(): array
182182
return [
183183
'prefix-match' => static fn () => Resolver\PrefixMatchingResolver::fromFlatMap($map),
184184
'prefix-match(file)' => static function () use ($map, $file_cache) {
185-
$prefix_map = $file_cache->parsing(function () use ($map) {
185+
$prefix_map = $file_cache->get(__FILE__, function () use ($map) {
186186
return Dict\map($map, fn ($v) => PrefixMatching\PrefixMap::fromFlatMap($v));
187187
});
188188

189189
return new Resolver\PrefixMatchingResolver($prefix_map);
190190
},
191191
'prefix-match(apcu)' => static function () use ($map, $apcu_cache) {
192-
$prefix_map = $apcu_cache->parsing(function () use ($map) {
192+
$prefix_map = $apcu_cache->get(__FILE__, function () use ($map) {
193193
return Dict\map($map, fn ($v) => PrefixMatching\PrefixMap::fromFlatMap($v));
194194
});
195195

196196
return new Resolver\PrefixMatchingResolver($prefix_map);
197197
},
198198
'prefix-match(memory)' => static function () use ($map, $memory_cache) {
199-
$prefix_map = $memory_cache->parsing(function () use ($map) {
199+
$prefix_map = $memory_cache->get(__FILE__, function () use ($map) {
200200
return Dict\map($map, fn ($v) => PrefixMatching\PrefixMap::fromFlatMap($v));
201201
});
202202

src/AbstractRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function getResolver(): Resolver\ResolverInterface
114114
return $this->resolver;
115115
}
116116

117-
$routes = $this->cache->parsing(function (): array {
117+
$routes = $this->cache->get(__FILE__, function (): array {
118118
return Dict\map(
119119
$this->getRoutes(),
120120
/**

src/Cache/ApcuCache.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,26 @@
1515
*/
1616
final class ApcuCache implements CacheInterface
1717
{
18-
private string $identifier;
19-
2018
public function __construct()
2119
{
2220
Psl\invariant(function_exists('apcu_fetch'), 'APCU extension is required to use "%s".', __CLASS__);
23-
24-
$this->identifier = SecureRandom\string(8);
2521
}
2622

2723
/**
28-
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $parser
24+
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $callback
2925
*
3026
* @return array<non-empty-string, PrefixMap<TResponder>>
3127
*/
32-
public function parsing(callable $parser): array
28+
public function get(string $item, callable $callback): array
3329
{
34-
$item = '/hack-routing/' . $this->identifier . '/parsing';
30+
$item = '/hack-routing/' . $item . '/prefix-map';
3531
/** @var false|array<non-empty-string, PrefixMap<TResponder>> $result */
3632
$result = apcu_fetch($item, $success);
3733
if ($success && false !== $result) {
3834
return $result;
3935
}
4036

41-
$result = $parser();
37+
$result = $callback();
4238
apcu_add($item, $result);
4339

4440
return $result;

src/Cache/CacheInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
interface CacheInterface
1313
{
1414
/**
15-
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $parser
15+
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $callback
1616
*
1717
* @return array<non-empty-string, PrefixMap<TResponder>>
1818
*/
19-
public function parsing(callable $parser): array;
19+
public function get(string $item, callable $callback): array;
2020
}

src/Cache/FileCache.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Psl;
99
use Psl\Env;
1010
use Psl\Filesystem;
11-
use Psl\SecureRandom;
11+
12+
use function md5;
13+
use function file_exists;
1214

1315
/**
1416
* @template TResponder
@@ -27,25 +29,25 @@ public function __construct(?string $directory = null)
2729
(string) $directory
2830
);
2931

30-
$this->directory = $directory ?? (Env\temp_dir() . '/hack-routing-' . SecureRandom\string(8));
32+
$this->directory = $directory ?? Env\temp_dir() . '/hack-routing';
3133
}
3234

3335
/**
34-
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $parser
36+
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $callback
3537
*
3638
* @return array<non-empty-string, PrefixMap<TResponder>>
3739
*/
38-
public function parsing(callable $parser): array
40+
public function get(string $item, callable $callback): array
3941
{
40-
$file = $this->directory . '/parsing.php';
41-
if (Filesystem\exists($file)) {
42+
$file = $this->directory . '/' . md5($item) . '/prefix-map.php';
43+
if (file_exists($file)) {
4244
/**
4345
* @psalm-suppress UnresolvableInclude
4446
* @var array<non-empty-string, PrefixMap<TResponder>> $result
4547
*/
4648
$result = require $file;
4749
} else {
48-
$result = $parser();
50+
$result = $callback();
4951
Filesystem\write_file($file, "<?php return unserialize('" . serialize($result) . "');");
5052
}
5153

src/Cache/MemoryCache.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@
1414
final class MemoryCache implements CacheInterface
1515
{
1616
/**
17-
* @var array<non-empty-string, PrefixMap<TResponder>>
17+
* @var array<string, array<non-empty-string, PrefixMap<TResponder>>>
1818
*/
19-
private ?array $parse_cache = null;
19+
private array $parse_cache = [];
2020

2121
/**
22-
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $parser
22+
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $callback
2323
*
2424
* @return array<non-empty-string, PrefixMap<TResponder>>
2525
*/
26-
public function parsing(callable $parser): array
26+
public function get(string $item, callable $callback): array
2727
{
28-
if (null === $this->parse_cache) {
29-
$this->parse_cache = $parser();
30-
}
31-
32-
return $this->parse_cache;
28+
return $this->parse_cache[$item] ?? ($this->parse_cache[$item] = $callback());
3329
}
3430
}

src/Cache/NullCache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
final class NullCache implements CacheInterface
1515
{
1616
/**
17-
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $parser
17+
* @param (callable(): array<non-empty-string, PrefixMap<TResponder>>) $callback
1818
*
1919
* @return array<non-empty-string, PrefixMap<TResponder>>
2020
*/
21-
public function parsing(callable $parser): array
21+
public function get(string $item, callable $callback): array
2222
{
23-
return $parser();
23+
return $callback();
2424
}
2525
}

src/Router.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace HackRouting;
66

7-
use Psl\Str;
8-
97
/**
108
* @template TResponder
119
*
@@ -35,7 +33,7 @@ protected function getRoutes(): array
3533
*/
3634
public function addRoute(string $method, string $route, mixed $responder): Router
3735
{
38-
$this->routes[Str\uppercase($method)][$route] = $responder;
36+
$this->routes[$method][$route] = $responder;
3937

4038
return $this;
4139
}

0 commit comments

Comments
 (0)