Add metadata and config changes to cache invalidation

Improve cache invalidation by including metadata.ini and config file
mtimes in the cache key calculation. This ensures cache entries are
invalidated when either the content file, its metadata, or global
configuration changes.
This commit is contained in:
Ruben 2026-02-07 16:12:21 +01:00
parent 67e6f1269a
commit 9be457f17f
2 changed files with 26 additions and 13 deletions

View file

@ -1,10 +1,23 @@
<?php <?php
function buildCacheKey(string $filePath, string $langPrefix = ''): string {
$mtime = filemtime($filePath);
// Include metadata.ini mtime so metadata edits invalidate cache
$metadataFile = dirname($filePath) . '/metadata.ini';
$metaMtime = file_exists($metadataFile) ? filemtime($metadataFile) : 0;
// Include global config mtime so config changes invalidate cache
$customConfig = __DIR__ . '/../custom/config.ini';
$defaultConfig = __DIR__ . '/default/config.ini';
$configMtime = file_exists($customConfig) ? filemtime($customConfig) : filemtime($defaultConfig);
return md5($filePath . $mtime . $metaMtime . $configMtime . $langPrefix);
}
function getCachedMarkdown(string $filePath, string $langPrefix = ''): ?string { function getCachedMarkdown(string $filePath, string $langPrefix = ''): ?string {
$cacheDir = '/tmp/folderweb_cache'; $cacheDir = '/tmp/folderweb_cache';
$mtime = filemtime($filePath); $cachePath = "$cacheDir/" . buildCacheKey($filePath, $langPrefix);
$cacheKey = md5($filePath . $mtime . $langPrefix);
$cachePath = "$cacheDir/$cacheKey";
if (file_exists($cachePath)) { if (file_exists($cachePath)) {
return file_get_contents($cachePath); return file_get_contents($cachePath);
@ -19,9 +32,7 @@ function setCachedMarkdown(string $filePath, string $html, string $langPrefix =
mkdir($cacheDir, 0755, true); mkdir($cacheDir, 0755, true);
} }
$mtime = filemtime($filePath); $cachePath = "$cacheDir/" . buildCacheKey($filePath, $langPrefix);
$cacheKey = md5($filePath . $mtime . $langPrefix);
$cachePath = "$cacheDir/$cacheKey";
file_put_contents($cachePath, $html); file_put_contents($cachePath, $html);
} }

View file

@ -61,12 +61,14 @@ Feed piggybacks on the existing Markdown cache — no separate feed cache needed
Defined in `app/cache.php`. File-based cache in `/tmp/folderweb_cache/`. Defined in `app/cache.php`. File-based cache in `/tmp/folderweb_cache/`.
**Cache key:** `md5($filePath . $mtime . $langPrefix)` **Cache key:** `md5($filePath . $mtime . $metaMtime . $configMtime . $langPrefix)`
- Invalidates when file is modified (mtime changes) - Invalidates when content file is modified (mtime changes)
- Invalidates when `metadata.ini` in the same directory is modified
- Invalidates when global config (`custom/config.ini` or `app/default/config.ini`) is modified
- Invalidates per-language (different link rewriting) - Invalidates per-language (different link rewriting)
- No explicit TTL — entries persist until temp directory cleanup - No explicit TTL — entries persist until temp directory cleanup
- **Does not track plugin state** — if a plugin modifies Markdown output (e.g., via PROCESS_CONTENT on files), changing plugin config won't bust the cache. Clear `/tmp/folderweb_cache/` manually after plugin changes that affect rendered Markdown. - **Does not track plugin state** — if a plugin modifies Markdown output (e.g., via PROCESS_CONTENT on files), changing plugin code won't bust the cache. Clear `/tmp/folderweb_cache/` manually after plugin changes that affect rendered Markdown.
```php ```php
getCachedMarkdown(string $filePath, string $langPrefix = ''): ?string getCachedMarkdown(string $filePath, string $langPrefix = ''): ?string