Skip to content

Commit 355f6c3

Browse files
chore(poc): decrease memory consumption at first call
1 parent 66ff923 commit 355f6c3

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/Application/Regulation/DatexGeneratorInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
interface DatexGeneratorInterface
88
{
99
public function generate(): void;
10+
11+
public function getCachedDatex(): string;
1012
}

src/Infrastructure/Adapter/DatexGenerator.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,37 @@ public function generate(): void
2828
new GetRegulationOrdersToDatexFormatQuery(),
2929
);
3030

31-
$content = $this->twig->render('api/regulations.xml.twig', [
32-
'publicationTime' => $this->dateUtils->getNow(),
33-
'regulationOrders' => $regulationOrders,
34-
]);
35-
3631
$dir = \dirname($this->datexFilePath);
3732

3833
if (!is_dir($dir)) {
3934
mkdir($dir, 0o755, true);
4035
}
4136

4237
$tmpFile = $this->datexFilePath . '.tmp';
43-
file_put_contents($tmpFile, $content);
38+
$handle = fopen($tmpFile, 'w');
39+
40+
ob_start(function (string $buffer) use ($handle): string {
41+
fwrite($handle, $buffer);
42+
43+
return '';
44+
}, 262144);
45+
46+
$this->twig->display('api/regulations.xml.twig', [
47+
'publicationTime' => $this->dateUtils->getNow(),
48+
'regulationOrders' => $regulationOrders,
49+
]);
50+
51+
ob_end_flush();
52+
fclose($handle);
4453
rename($tmpFile, $this->datexFilePath);
4554
}
4655

47-
public function getDatexFilePath(): string
56+
public function getCachedDatex(): string
4857
{
49-
return $this->datexFilePath;
58+
if (!file_exists($this->datexFilePath)) {
59+
$this->generate();
60+
}
61+
62+
return file_get_contents($this->datexFilePath);
5063
}
5164
}

src/Infrastructure/Controller/Api/Regulations/GetRegulationsController.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use App\Application\DateUtilsInterface;
88
use App\Application\QueryBusInterface;
9+
use App\Application\Regulation\DatexGeneratorInterface;
910
use App\Application\Regulation\Query\GetRegulationOrdersToDatexFormatQuery;
10-
use App\Infrastructure\Adapter\DatexGenerator;
1111
use OpenApi\Attributes as OA;
1212
use Symfony\Component\HttpFoundation\Response;
1313
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -20,7 +20,7 @@ public function __construct(
2020
private \Twig\Environment $twig,
2121
private DateUtilsInterface $dateUtils,
2222
private QueryBusInterface $queryBus,
23-
private DatexGenerator $datexGenerator,
23+
private DatexGeneratorInterface $datexGenerator,
2424
) {
2525
}
2626

@@ -40,15 +40,12 @@ public function __invoke(
4040
bool $includeExpired = false,
4141
): Response {
4242
$isDefaultParams = $includePermanent && $includeTemporary && !$includeExpired;
43-
$datexFilePath = $this->datexGenerator->getDatexFilePath();
4443

4544
if ($isDefaultParams) {
46-
if (!file_exists($datexFilePath)) {
47-
$this->datexGenerator->generate();
48-
}
45+
$regulationOrders = $this->datexGenerator->getCachedDatex();
4946

5047
return new Response(
51-
file_get_contents($datexFilePath),
48+
$regulationOrders,
5249
Response::HTTP_OK,
5350
['Content-Type' => 'text/xml; charset=UTF-8'],
5451
);

0 commit comments

Comments
 (0)