folderweb/app/cache.php

39 lines
1.3 KiB
PHP
Raw Normal View History

<?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';
$cachePath = "$cacheDir/" . buildCacheKey($filePath, $langPrefix);
if (file_exists($cachePath)) {
return file_get_contents($cachePath);
}
return null;
}
function setCachedMarkdown(string $filePath, string $html, string $langPrefix = ''): void {
$cacheDir = '/tmp/folderweb_cache';
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
$cachePath = "$cacheDir/" . buildCacheKey($filePath, $langPrefix);
file_put_contents($cachePath, $html);
}