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,15 +1,28 @@
<?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 {
$cacheDir = '/tmp/folderweb_cache';
$mtime = filemtime($filePath);
$cacheKey = md5($filePath . $mtime . $langPrefix);
$cachePath = "$cacheDir/$cacheKey";
$cachePath = "$cacheDir/" . buildCacheKey($filePath, $langPrefix);
if (file_exists($cachePath)) {
return file_get_contents($cachePath);
}
return null;
}
@ -18,10 +31,8 @@ function setCachedMarkdown(string $filePath, string $html, string $langPrefix =
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
$mtime = filemtime($filePath);
$cacheKey = md5($filePath . $mtime . $langPrefix);
$cachePath = "$cacheDir/$cacheKey";
$cachePath = "$cacheDir/" . buildCacheKey($filePath, $langPrefix);
file_put_contents($cachePath, $html);
}