Skip to content

Commit aae5891

Browse files
committed
Add cache_check_mtime configuration and update CacheManager to respect it; enhance tests for cache key generation
1 parent 9246bc5 commit aae5891

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎src/Internal/Cli/ConfigInitCommand.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ private static function getTemplate(): string
153153
'cache' => true,
154154
'cache_dir' => null,
155155
156+
/*
157+
|--------------------------------------------------------------------------
158+
| Cache File Modification Monitor
159+
|--------------------------------------------------------------------------
160+
| When enabled (default), TypePHP checks file modification times (filemtime)
161+
| to automatically rebuild the cache when a file changes.
162+
|
163+
| In production, files do not change. Set this to FALSE to eliminate
164+
| hundreds of disk I/O checks per request for maximum performance.
165+
| Note: If disabled, you must run `php bin/typephp cache:clear` on deployment.
166+
*/
167+
'cache_check_mtime' => true,
168+
156169
/*
157170
|--------------------------------------------------------------------------
158171
| Registered Extensions

‎src/Internal/Io/CacheManager.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ public static function getCacheDir(): string
7272
*/
7373
public static function getCacheKey(string $resolvedPath): string
7474
{
75-
$mtime = @filemtime($resolvedPath);
76-
$mtimeStr = $mtime !== false ? (string) $mtime : '0';
75+
if (Config::isCacheCheckMtimeEnabled()) {
76+
$mtime = @filemtime($resolvedPath);
77+
$mtimeStr = $mtime !== false ? (string) $mtime : '0';
7778

78-
return hash('xxh128', self::VERSION_PREFIX . $resolvedPath . $mtimeStr);
79+
return hash('xxh128', self::VERSION_PREFIX . $resolvedPath . $mtimeStr);
80+
}
81+
82+
return hash('xxh128', self::VERSION_PREFIX . $resolvedPath);
7983
}
8084

8185
/**

‎src/Internal/Util/Config.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ final class Config
5656

5757
private static bool $vendorBoundaryOnly = true;
5858

59+
private static bool $cacheCheckMtime = true;
60+
5961
private static string $arrayValidation = 'full';
6062

6163
public static function isEnabled(): bool
@@ -67,6 +69,15 @@ public static function isEnabled(): bool
6769
return self::$enabled;
6870
}
6971

72+
public static function isCacheCheckMtimeEnabled(): bool
73+
{
74+
if (self::$cachedConfig === null) {
75+
self::get();
76+
}
77+
78+
return self::$cacheCheckMtime;
79+
}
80+
7081
public static function isParamsEnabled(): bool
7182
{
7283
if (self::$cachedConfig === null) {
@@ -236,6 +247,7 @@ public static function get(): array
236247
'array_validation' => 'full',
237248
'cache' => true,
238249
'cache_dir' => null,
250+
'cache_check_mtime' => true,
239251
'inline_vars' => [
240252
'properties' => true,
241253
'generics' => true,
@@ -376,6 +388,7 @@ public static function reset(): void
376388
self::$respectNativeNullability = true;
377389
self::$vendorBoundaryOnly = true;
378390
self::$arrayValidation = 'full';
391+
self::$cacheCheckMtime = true;
379392

380393
DocblockParser::reset();
381394
ParamChecker::reset();
@@ -410,5 +423,6 @@ private static function syncFlags(array $config): void
410423
self::$respectNativeNullability = (bool) ($config['respect_native_nullability'] ?? true);
411424
self::$vendorBoundaryOnly = (bool) ($config['vendor_boundary_only'] ?? true);
412425
self::$arrayValidation = \is_string($config['array_validation'] ?? null) ? $config['array_validation'] : 'full';
426+
self::$cacheCheckMtime = (bool) ($config['cache_check_mtime'] ?? true);
413427
}
414428
}

‎tests/Internal/Io/CacheManagerTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use TypePHP\Internal\Io\CacheManager;
6+
use TypePHP\Internal\Util\Config;
67

78
describe('CacheManager Unit Tests', function () {
89
test('returns valid cache directory path', function () {
@@ -29,4 +30,21 @@
2930
->and(file_exists($testFile))->toBeFalse()
3031
;
3132
});
33+
34+
test('bypasses filemtime when cache_check_mtime is disabled', function () {
35+
try {
36+
$file = __FILE__;
37+
38+
Config::set(['cache_check_mtime' => true]);
39+
$keyWithMtime = CacheManager::getCacheKey($file);
40+
41+
Config::set(['cache_check_mtime' => false]);
42+
$keyWithoutMtime = CacheManager::getCacheKey($file);
43+
44+
expect($keyWithMtime)->not()->toBe($keyWithoutMtime);
45+
expect(CacheManager::getCacheKey($file))->toBe($keyWithoutMtime);
46+
} finally {
47+
Config::reset();
48+
}
49+
});
3250
});

0 commit comments

Comments
 (0)