-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-file-cache-strategy.php
More file actions
66 lines (51 loc) · 2.94 KB
/
05-file-cache-strategy.php
File metadata and controls
66 lines (51 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
/**
* Example 05: FileCacheStrategy — cache scan results to avoid re-scanning.
*
* Demonstrates: FileCacheStrategy, performance comparison cold vs warm cache.
*
* !! PARAMETER REFERENCE (confirmed from implementations):
*
* FileCacheStrategy(cachePath: string, ttl: int, version: ?string):
* - cachePath : directory for .php cache files (created automatically)
* - ttl : cache time-to-live in seconds (default 3600)
* - version : if provided, old cache files with different version are invalidated
* ⚠️ Cache is invalidated automatically when scanned file mtimes change.
*
* $scanner->scan(dirs, filters, cache: CacheStrategy): DiscoveryResult
* - cache: pass a CacheStrategy to enable scan result caching
*/
require_once __DIR__ . '/../vendor/autoload.php';
use KaririCode\ClassDiscovery\Cache\FileCacheStrategy;
use KaririCode\ClassDiscovery\Scanner\ComposerNamespaceResolver;
use KaririCode\ClassDiscovery\Scanner\FileScanner;
echo "\n";
echo "╔══════════════════════════════════════════════════════════╗\n";
echo "║ Example 05: File-based Cache Strategy ║\n";
echo "╚══════════════════════════════════════════════════════════╝\n\n";
$cacheDir = sys_get_temp_dir() . '/kariricode-classdiscovery-example-cache';
$cache = new FileCacheStrategy($cacheDir);
$resolver = new ComposerNamespaceResolver(
composerJsonPath: __DIR__ . '/../composer.json',
);
// ── Cold scan (no cache) ────────────────────────────────────────
$cache->clear();
$scanner = new FileScanner($resolver);
$scanner->setCacheStrategy($cache);
$coldScanStart = hrtime(true);
$result = $scanner->scan([__DIR__ . '/../src']);
$coldMs = (hrtime(true) - $coldScanStart) / 1_000_000;
echo "❄ Cold scan : " . count($result) . " class(es) in " . round($coldMs, 3) . "ms\n";
// ── Warm scan (from cache) ──────────────────────────────────────
$warmScanner = new FileScanner($resolver);
$warmScanner->setCacheStrategy($cache);
$warmScanStart = hrtime(true);
$warmResult = $warmScanner->scan([__DIR__ . '/../src']);
$warmMs = (hrtime(true) - $warmScanStart) / 1_000_000;
echo "🔥 Warm scan : " . count($warmResult) . " class(es) in " . round($warmMs, 3) . "ms\n";
$speedup = $coldMs > 0 ? round($coldMs / max($warmMs, 0.001), 1) : '∞';
echo "⚡ Speedup : {$speedup}×\n";
// ── Cleanup ─────────────────────────────────────────────────────
$cache->clear();
echo "\n ✅ FileCacheStrategy: OK\n\n";