Some checks failed
Deploy Application / deploy (push) Has been cancelled
227 lines
6.6 KiB
Markdown
227 lines
6.6 KiB
Markdown
# Weitere Refactoring-Vorschläge für Discovery Cache
|
|
|
|
## Implementiert: Vorschlag 2 - Cache-Invalidierung bei Datei-Änderungen
|
|
|
|
✅ **Implementiert:**
|
|
- `StalenessChecker::areFilesModifiedSince()` - Prüft einzelne Dateien statt nur Verzeichnisse
|
|
- `DiscoveryCacheManager::invalidateIfFilesChanged()` - Invalidiert Cache bei Datei-Änderungen
|
|
- `DiscoveryCacheManager::extractCriticalFilesFromRegistry()` - Extrahiert kritische Dateien aus Registry
|
|
- Integration in `DiscoveryServiceBootstrapper` für automatische Prüfung kritischer Komponenten-Dateien
|
|
|
|
## Weitere Vorschläge
|
|
|
|
### Vorschlag 6: Automatische Datei-Erkennung für kritische Komponenten
|
|
|
|
**Problem:** Aktuell müssen kritische Dateien manuell in `DiscoveryServiceBootstrapper` aufgelistet werden.
|
|
|
|
**Lösung:** Automatische Erkennung aller Komponenten-Dateien aus der Registry:
|
|
|
|
```php
|
|
// In DiscoveryCacheManager.php
|
|
public function invalidateIfComponentFilesChanged(DiscoveryContext $context): bool
|
|
{
|
|
$result = $this->getStandardCache($context);
|
|
|
|
if (!$result->found || !$result->entry) {
|
|
return false;
|
|
}
|
|
|
|
$criticalFiles = $this->extractCriticalFilesFromRegistry($result->entry->registry);
|
|
|
|
if (empty($criticalFiles)) {
|
|
return false;
|
|
}
|
|
|
|
return $this->invalidateIfFilesChanged($context, $criticalFiles);
|
|
}
|
|
```
|
|
|
|
**Vorteil:** Keine manuelle Wartung der Datei-Liste nötig.
|
|
|
|
### Vorschlag 7: Watch-Mode für Development
|
|
|
|
**Problem:** In Development müssen Entwickler manuell den Cache löschen.
|
|
|
|
**Lösung:** Watch-Mode, der automatisch auf Datei-Änderungen reagiert:
|
|
|
|
```php
|
|
// In DiscoveryServiceBootstrapper.php
|
|
public function enableWatchMode(array $watchPaths = []): void
|
|
{
|
|
$this->watchMode = true;
|
|
$this->watchPaths = $watchPaths;
|
|
|
|
// Register file watcher
|
|
$this->fileWatcher = new FileWatcher($watchPaths, function($changedFiles) {
|
|
$this->invalidateCacheForFiles($changedFiles);
|
|
});
|
|
}
|
|
```
|
|
|
|
**Vorteil:** Automatische Cache-Invalidierung während Development.
|
|
|
|
### Vorschlag 8: Cache-Warming mit File-Hash-Verification
|
|
|
|
**Problem:** Cache könnte veraltet sein, auch wenn Datei-Modifikationszeiten gleich sind.
|
|
|
|
**Lösung:** File-Hash-Verification für kritische Dateien:
|
|
|
|
```php
|
|
// In StalenessChecker.php
|
|
public function verifyFileHashes(array $filePaths, array $expectedHashes): bool
|
|
{
|
|
foreach ($filePaths as $filePath) {
|
|
if (!isset($expectedHashes[$filePath])) {
|
|
continue;
|
|
}
|
|
|
|
$currentHash = md5_file($filePath);
|
|
if ($currentHash !== $expectedHashes[$filePath]) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
```
|
|
|
|
**Vorteil:** Präzisere Erkennung von Datei-Änderungen, auch bei gleichen Modifikationszeiten.
|
|
|
|
### Vorschlag 9: Incremental Cache Updates
|
|
|
|
**Problem:** Bei jeder Datei-Änderung wird der gesamte Cache neu erstellt.
|
|
|
|
**Lösung:** Incremental Updates - nur geänderte Komponenten werden neu geladen:
|
|
|
|
```php
|
|
// In DiscoveryCacheManager.php
|
|
public function updateCacheIncrementally(DiscoveryContext $context, array $changedFiles): bool
|
|
{
|
|
$result = $this->getStandardCache($context);
|
|
|
|
if (!$result->found || !$result->entry) {
|
|
return false;
|
|
}
|
|
|
|
$registry = $result->entry->registry;
|
|
|
|
// Remove entries for changed files
|
|
foreach ($changedFiles as $file) {
|
|
$registry = $this->removeEntriesForFile($registry, $file);
|
|
}
|
|
|
|
// Re-discover only changed files
|
|
$newEntries = $this->discoverFiles($changedFiles);
|
|
|
|
// Merge new entries into registry
|
|
$updatedRegistry = $this->mergeRegistry($registry, $newEntries);
|
|
|
|
// Store updated registry
|
|
return $this->store($context, $updatedRegistry);
|
|
}
|
|
```
|
|
|
|
**Vorteil:** Deutlich schneller bei kleinen Änderungen.
|
|
|
|
### Vorschlag 10: Cache-Tags für selektive Invalidierung
|
|
|
|
**Problem:** Bei Änderungen müssen alle Discovery-Caches gelöscht werden.
|
|
|
|
**Lösung:** Cache-Tags für selektive Invalidierung:
|
|
|
|
```php
|
|
// In DiscoveryCacheManager.php
|
|
public function invalidateByTag(string $tag): bool
|
|
{
|
|
// Invalidate only caches with specific tag
|
|
// e.g., 'component:popover', 'route:api', etc.
|
|
$keys = $this->findCacheKeysByTag($tag);
|
|
|
|
foreach ($keys as $key) {
|
|
$this->cache->forget($key);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
```
|
|
|
|
**Vorteil:** Selektive Invalidierung nur betroffener Caches.
|
|
|
|
### Vorschlag 11: Cache-Metadaten für Debugging
|
|
|
|
**Problem:** Schwer zu debuggen, welche Dateien im Cache enthalten sind.
|
|
|
|
**Lösung:** Cache-Metadaten mit Datei-Liste:
|
|
|
|
```php
|
|
// In CacheEntry.php
|
|
public function __construct(
|
|
public readonly DiscoveryRegistry|array $registry,
|
|
public readonly Timestamp $createdAt,
|
|
public readonly string $version = '',
|
|
public readonly CacheLevel $cacheLevel = CacheLevel::NORMAL,
|
|
public readonly CacheTier $cacheTier = CacheTier::HOT,
|
|
public readonly array $sourceFiles = [], // NEW: List of source files
|
|
public readonly array $fileHashes = [] // NEW: File hashes for verification
|
|
) {
|
|
}
|
|
```
|
|
|
|
**Vorteil:** Besseres Debugging und Verifikation.
|
|
|
|
### Vorschlag 12: Background Cache Refresh
|
|
|
|
**Problem:** Cache-Refresh blockiert Request.
|
|
|
|
**Lösung:** Background Refresh mit stale-while-revalidate Pattern:
|
|
|
|
```php
|
|
// In DiscoveryCacheManager.php
|
|
public function getWithBackgroundRefresh(DiscoveryContext $context): ?DiscoveryRegistry
|
|
{
|
|
$result = $this->getStandardCache($context);
|
|
|
|
if ($result->found && $result->isUsable()) {
|
|
return $result->entry->registry;
|
|
}
|
|
|
|
// Return stale cache if available
|
|
if ($result->entry !== null) {
|
|
$this->triggerBackgroundRefresh($context);
|
|
return $result->entry->registry;
|
|
}
|
|
|
|
// No cache available, do synchronous refresh
|
|
return null;
|
|
}
|
|
```
|
|
|
|
**Vorteil:** Keine Blockierung von Requests während Cache-Refresh.
|
|
|
|
## Empfohlene Implementierungsreihenfolge
|
|
|
|
1. ✅ **Vorschlag 2** (Cache-Invalidierung bei Datei-Änderungen) - **IMPLEMENTIERT**
|
|
2. **Vorschlag 6** (Automatische Datei-Erkennung) - Sofortige Verbesserung
|
|
3. **Vorschlag 11** (Cache-Metadaten) - Für besseres Debugging
|
|
4. **Vorschlag 7** (Watch-Mode) - Für Development-Quality-of-Life
|
|
5. **Vorschlag 9** (Incremental Updates) - Für Performance
|
|
6. **Vorschlag 10** (Cache-Tags) - Für Skalierbarkeit
|
|
7. **Vorschlag 12** (Background Refresh) - Für Production-Performance
|
|
8. **Vorschlag 8** (File-Hash-Verification) - Optional, für höchste Präzision
|
|
|
|
## Priorisierung
|
|
|
|
**Hoch (sofort):**
|
|
- Vorschlag 6: Automatische Datei-Erkennung
|
|
- Vorschlag 11: Cache-Metadaten
|
|
|
|
**Mittel (nächste Iteration):**
|
|
- Vorschlag 7: Watch-Mode
|
|
- Vorschlag 9: Incremental Updates
|
|
|
|
**Niedrig (langfristig):**
|
|
- Vorschlag 10: Cache-Tags
|
|
- Vorschlag 12: Background Refresh
|
|
- Vorschlag 8: File-Hash-Verification
|
|
|