getCacheKey($templatePath); // Try cache first $cacheItem = $this->cache->get($cacheKey); if ($cacheItem->isHit) { $compiled = CompiledTemplate::fromArray($cacheItem->value); // Check staleness if (! $this->isStale($compiled, $templatePath)) { return $compiled; } } // Cache miss or stale - compile and cache $templateContent = file_get_contents($templatePath); if ($templateContent === false) { throw new \RuntimeException("Failed to read template: {$templatePath}"); } $compiled = $compiler($templateContent); // Cache with template file modification time $this->cache->set( CacheItem::forSet( key: $cacheKey, value: $compiled->toArray(), ttl: Duration::fromHours(self::CACHE_TTL_HOURS) ) ); return $compiled; } /** * Prüft ob kompiliertes Template veraltet ist */ private function isStale(CompiledTemplate $compiled, string $templatePath): bool { if (! file_exists($templatePath)) { return true; } $fileModifiedTime = filemtime($templatePath); if ($fileModifiedTime === false) { return true; } return $fileModifiedTime > $compiled->compiledAt; } /** * Invalidiert Cache für spezifisches Template */ public function invalidate(string $templatePath): bool { $cacheKey = $this->getCacheKey($templatePath); return $this->cache->forget($cacheKey); } /** * Generiert Cache-Key für Template */ private function getCacheKey(string $templatePath): CacheKey { $hash = Hash::sha256($templatePath); return CacheKey::fromString(self::CACHE_KEY_PREFIX . $hash->toShort(16)); } }