Skip to content

Commit 231f055

Browse files
committed
Enable/disable the cache deletion from ENV
- By default cache is disabled
1 parent aa2b3a3 commit 231f055

4 files changed

Lines changed: 59 additions & 9 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
/var
33
/composer.lock
4-
/.php_cs.cache
4+
/.php_cs.cache
5+
/.phpunit.result.cache

AsyncKernel.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Drift\HttpKernel\DependencyInjection\CompilerPass\FilesystemCompilerPass;
2222
use Drift\HttpKernel\DependencyInjection\CompilerPass\PeriodicTimersCompilerPass;
2323
use Drift\HttpKernel\Exception\AsyncHttpKernelNeededException;
24+
use Drift\HttpKernel\PeriodicTimer\PeriodicTimer;
2425
use Exception;
2526
use React\Promise\PromiseInterface;
2627
use Symfony\Component\Filesystem\Filesystem;
@@ -46,14 +47,17 @@ public function boot()
4647
{
4748
if (!$this->booted) {
4849
$this->uid = $this->generateUID();
49-
$fs = new Filesystem();
50-
// AsyncKernel loads the container only once when it loads. Storing it in the filesystem is not for cache purposes
51-
// but more for using the same loading process as Kernel class use.
52-
// Hence, everytime before AsyncKernel initiates the container it deletes the cache dir,
53-
// to make sure it is building the updated kernel
54-
$cachePath = $this->getCacheDir();
55-
if ($fs->exists($cachePath)) {
56-
$fs->remove($cachePath);
50+
51+
if (($_ENV['DRIFT_CACHE_ENABLED'] ?? '0') !== '1') {
52+
$fs = new Filesystem();
53+
// AsyncKernel loads the container only once when it loads. Storing it in the filesystem is not for cache purposes
54+
// but more for using the same loading process as Kernel class use.
55+
// Hence, everytime before AsyncKernel initiates the container it deletes the cache dir,
56+
// to make sure it is building the updated kernel
57+
$cachePath = $this->getCacheDir();
58+
if ($fs->exists($cachePath)) {
59+
$fs->remove($cachePath);
60+
}
5761
}
5862
}
5963

Tests/Base/KernelCacheTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
4+
namespace Drift\HttpKernel\Tests\Base;
5+
6+
use Drift\HttpKernel\Tests\AsyncKernelFunctionalTest;
7+
8+
/**
9+
* Class KernelCacheTest
10+
*/
11+
class KernelCacheTest extends AsyncKernelFunctionalTest
12+
{
13+
public function testWithoutCache()
14+
{
15+
$_ENV['DRIFT_CACHE_ENABLED'] = '0';
16+
static::setUpBeforeClass();
17+
$cacheDir = self::$kernel->getCacheDir();
18+
$firstKernelCacheCreationTime = lstat($cacheDir)['ctime'];
19+
sleep(1);
20+
21+
static::setUpBeforeClass();
22+
$cacheDir = self::$kernel->getCacheDir();
23+
$secondKernelCacheCreationTime = lstat($cacheDir)['ctime'];
24+
25+
$this->assertGreaterThan($firstKernelCacheCreationTime, $secondKernelCacheCreationTime);
26+
unset($_ENV['DRIFT_CACHE_ENABLED']);
27+
}
28+
29+
public function testWithCache()
30+
{
31+
$_ENV['DRIFT_CACHE_ENABLED'] = '1';
32+
static::setUpBeforeClass();
33+
$cacheDir = self::$kernel->getCacheDir();
34+
$firstKernelCacheCreationTime = lstat($cacheDir)['ctime'];
35+
sleep(1);
36+
37+
static::setUpBeforeClass();
38+
$cacheDir = self::$kernel->getCacheDir();
39+
$secondKernelCacheCreationTime = lstat($cacheDir)['ctime'];
40+
41+
$this->assertEquals($firstKernelCacheCreationTime, $secondKernelCacheCreationTime);
42+
unset($_ENV['DRIFT_CACHE_ENABLED']);
43+
}
44+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"symfony/http-kernel": "^5.0",
1616
"symfony/dependency-injection": "^5.0",
17+
"symfony/filesystem": "^5.0",
1718
"symfony/event-dispatcher-contracts": "^2.0",
1819
"react/promise": "^2.7",
1920
"react/http": "^1.0",

0 commit comments

Comments
 (0)