Add markdown file caching functionality
Add cache.php with get/set functions for markdown content Update rendering to check cache before processing files
This commit is contained in:
parent
3e303ceda8
commit
6009cb451e
2 changed files with 39 additions and 3 deletions
27
app/cache.php
Normal file
27
app/cache.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
function getCachedMarkdown(string $filePath): ?string {
|
||||
$cacheDir = '/tmp/folderweb_cache';
|
||||
$mtime = filemtime($filePath);
|
||||
$cacheKey = md5($filePath . $mtime);
|
||||
$cachePath = "$cacheDir/$cacheKey";
|
||||
|
||||
if (file_exists($cachePath)) {
|
||||
return file_get_contents($cachePath);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function setCachedMarkdown(string $filePath, string $html): void {
|
||||
$cacheDir = '/tmp/folderweb_cache';
|
||||
if (!is_dir($cacheDir)) {
|
||||
mkdir($cacheDir, 0755, true);
|
||||
}
|
||||
|
||||
$mtime = filemtime($filePath);
|
||||
$cacheKey = md5($filePath . $mtime);
|
||||
$cachePath = "$cacheDir/$cacheKey";
|
||||
|
||||
file_put_contents($cachePath, $html);
|
||||
}
|
||||
|
|
@ -5,10 +5,19 @@ function renderContentFile(string $filePath): string {
|
|||
|
||||
ob_start();
|
||||
if ($ext === 'md') {
|
||||
if (!class_exists('Parsedown')) {
|
||||
require_once __DIR__ . '/vendor/Parsedown.php';
|
||||
require_once __DIR__ . '/cache.php';
|
||||
$cached = getCachedMarkdown($filePath);
|
||||
|
||||
if ($cached !== null) {
|
||||
echo $cached;
|
||||
} else {
|
||||
if (!class_exists('Parsedown')) {
|
||||
require_once __DIR__ . '/vendor/Parsedown.php';
|
||||
}
|
||||
$html = '<article>' . (new Parsedown())->text(file_get_contents($filePath)) . '</article>';
|
||||
setCachedMarkdown($filePath, $html);
|
||||
echo $html;
|
||||
}
|
||||
echo '<article>' . (new Parsedown())->text(file_get_contents($filePath)) . '</article>';
|
||||
} elseif (in_array($ext, ['html', 'php'])) {
|
||||
include $filePath;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue