File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff line change 1515 */
1616final 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 ;
Original file line number Diff line number Diff line change 1212interface 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}
Original file line number Diff line number Diff line change 88use Psl ;
99use Psl \Env ;
1010use 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
Original file line number Diff line number Diff line change 1414final 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}
Original file line number Diff line number Diff line change 1414final 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}
Original file line number Diff line number Diff line change 44
55namespace 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 }
You can’t perform that action at this time.
0 commit comments