Skip to content

Commit d276dee

Browse files
committed
feat(caching): optimize caching by using pid and tmp dir
1 parent fa77e56 commit d276dee

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

config/container.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Psr\Http\Message\ResponseFactoryInterface;
1515
use Psr\Http\Message\ResponseInterface;
1616
use Psr\Http\Message\StreamFactoryInterface;
17+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
18+
use Psr\Cache\CacheItemPoolInterface;
1719
use WebProject\PhpOpenApiMockServer\Factory\OpenApiMockMiddlewareFactory;
1820
use WebProject\PhpOpenApiMockServer\Middleware\ForceMockActiveMiddleware;
1921

@@ -40,13 +42,26 @@
4042
$container->setService(ResponseFactoryInterface::class, new ResponseFactory());
4143
$container->setService(StreamFactoryInterface::class, new StreamFactory());
4244

43-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
44-
use Psr\Cache\CacheItemPoolInterface;
45+
// Cache Configuration — isolated per process to avoid conflicts between parallel server instances
46+
$processCacheDir = sys_get_temp_dir() . '/openapi_mock_cache/' . getmypid();
47+
$container->setFactory(CacheItemPoolInterface::class, static fn(): FilesystemAdapter => new FilesystemAdapter('openapi_mock', 0, $processCacheDir));
48+
49+
register_shutdown_function(static function () use ($processCacheDir): void {
50+
if (! is_dir($processCacheDir)) {
51+
return;
52+
}
53+
54+
$iterator = new RecursiveIteratorIterator(
55+
new RecursiveDirectoryIterator($processCacheDir, FilesystemIterator::SKIP_DOTS),
56+
RecursiveIteratorIterator::CHILD_FIRST,
57+
);
4558

46-
// ...
59+
foreach ($iterator as $path) {
60+
$path->isDir() ? rmdir($path->getPathname()) : unlink($path->getPathname());
61+
}
4762

48-
// Cache Configuration
49-
$container->setFactory(CacheItemPoolInterface::class, static fn(): FilesystemAdapter => new FilesystemAdapter('openapi_mock', 0, dirname(__DIR__) . '/data/cache'));
63+
rmdir($processCacheDir);
64+
});
5065

5166
// Application Middleware
5267
$container->setFactory(OpenApiMockMiddleware::class, OpenApiMockMiddlewareFactory::class);

data/cache/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

data/cache/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)