Skip to content

Commit ca3eb8e

Browse files
n.gnatosamizdam
authored andcommitted
Add CacheableDispatcherFactoryProxy
1 parent e084989 commit ca3eb8e

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Data Transfer Object classes generation from swagger spec
1111
- JsonApiResponseFactory::createResponse() `meta` and `links` arguments
12+
- CacheableDispatcherFactoryProxy
1213

1314
## [0.0.13] - 2021-01-27
1415
### Added

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
"neomerx/json-api": "^4.0",
2323
"nette/php-generator": "^3.4",
2424
"nikic/fast-route": "^1.3",
25-
"psr/http-message": "^1.0",
25+
"psr/cache": "^1.0",
2626
"psr/http-factory": "^1.0",
27+
"psr/http-message": "^1.0",
2728
"psr/http-server-handler": "^1.0",
2829
"psr/http-server-middleware": "^1.0",
2930
"rakit/validation": "^1.2",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace FreeElephants\JsonApiToolkit\Routing\FastRoute;
4+
5+
use FastRoute\Dispatcher;
6+
use Psr\Cache\CacheItemPoolInterface;
7+
8+
class CacheableDispatcherFactoryProxy implements DispatcherFactoryInterface
9+
{
10+
private DispatcherFactoryInterface $dispatcherFactory;
11+
private CacheItemPoolInterface $cacheItemPool;
12+
13+
public function __construct(DispatcherFactoryInterface $dispatcherFactory, CacheItemPoolInterface $cacheItemPool)
14+
{
15+
$this->dispatcherFactory = $dispatcherFactory;
16+
$this->cacheItemPool = $cacheItemPool;
17+
}
18+
19+
public function buildDispatcher(string $openApiDocumentSource): Dispatcher
20+
{
21+
$key = md5_file($openApiDocumentSource);
22+
$cacheItem = $this->cacheItemPool->getItem($key);
23+
if ($cacheItem->isHit()) {
24+
$dispatcher = $cacheItem->get();
25+
} else {
26+
$dispatcher = $this->dispatcherFactory->buildDispatcher($openApiDocumentSource);
27+
$cacheItem->set($dispatcher);
28+
$this->cacheItemPool->save($cacheItem);
29+
}
30+
31+
return $dispatcher;
32+
}
33+
}

0 commit comments

Comments
 (0)