Add file-based API cache — monitor and arrs pages now serve from /tmp/vv_cache instead of making live HTTP calls on every page load

This commit is contained in:
Gmer4Lfe
2026-06-03 16:15:35 -04:00
parent 9655a30d25
commit 65075ed599
5 changed files with 107 additions and 0 deletions
+6
View File
@@ -1,4 +1,10 @@
<?php
header('Content-Type: application/json');
require_once dirname(__DIR__) . '/include/config.php';
$_vv_cached = vv_cache_read('arrs', 90);
if ($_vv_cached !== null) { echo json_encode($_vv_cached); exit; }
unset($_vv_cached);
require_once dirname(__DIR__) . '/include/arrs.php';
echo json_encode(vv_arrs_all());
+6
View File
@@ -1,5 +1,11 @@
<?php
header('Content-Type: application/json');
require_once dirname(__DIR__) . '/include/config.php';
$_vv_cached = vv_cache_read('monitor', 90);
if ($_vv_cached !== null) { echo json_encode($_vv_cached); exit; }
unset($_vv_cached);
require_once dirname(__DIR__) . '/include/monitor.php';
require_once dirname(__DIR__) . '/include/vms.php';
require_once dirname(__DIR__) . '/include/docker_folders.php';
+21
View File
@@ -291,3 +291,24 @@ function vv_unraid_api_query(string $hostId, string $gql, int $timeoutSec = 5, s
// data key present (even if null means query ran but returned nothing useful).
return array_key_exists('data', $decoded ?? []) ? $decoded['data'] : null;
}
// ── File-based API cache (/tmp/vv_cache — tmpfs, cleared on reboot) ───────────
define('VV_CACHE_DIR', '/tmp/vv_cache');
// Read a cached payload. Returns null if missing or older than $maxAge seconds.
function vv_cache_read(string $key, int $maxAge = 90): ?array {
$f = VV_CACHE_DIR . '/' . $key . '.json';
if (!file_exists($f) || (time() - filemtime($f)) > $maxAge) return null;
$raw = file_get_contents($f);
return $raw ? (json_decode($raw, true) ?: null) : null;
}
// Write a payload atomically (tmp + rename) so readers never see a partial file.
function vv_cache_write(string $key, array $data): void {
if (!is_dir(VV_CACHE_DIR)) @mkdir(VV_CACHE_DIR, 0755, true);
$f = VV_CACHE_DIR . '/' . $key . '.json';
$tmp = $f . '.tmp';
file_put_contents($tmp, json_encode($data));
rename($tmp, $f);
}