From 65075ed599c7ef600524d738534bc06454b87c31 Mon Sep 17 00:00:00 2001 From: Gmer4Lfe Date: Wed, 3 Jun 2026 16:15:35 -0400 Subject: [PATCH] =?UTF-8?q?Add=20file-based=20API=20cache=20=E2=80=94=20mo?= =?UTF-8?q?nitor=20and=20arrs=20pages=20now=20serve=20from=20/tmp/vv=5Fcac?= =?UTF-8?q?he=20instead=20of=20making=20live=20HTTP=20calls=20on=20every?= =?UTF-8?q?=20page=20load?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plugin/unraid/api/arrs.php | 6 ++++ Plugin/unraid/api/monitor.php | 6 ++++ Plugin/unraid/include/config.php | 21 ++++++++++++ Tools/api_cache_writer.php | 56 ++++++++++++++++++++++++++++++++ Tools/api_cache_writer.sh | 18 ++++++++++ 5 files changed, 107 insertions(+) create mode 100644 Tools/api_cache_writer.php create mode 100755 Tools/api_cache_writer.sh diff --git a/Plugin/unraid/api/arrs.php b/Plugin/unraid/api/arrs.php index f65f3f2..08b7221 100644 --- a/Plugin/unraid/api/arrs.php +++ b/Plugin/unraid/api/arrs.php @@ -1,4 +1,10 @@ $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); +} diff --git a/Tools/api_cache_writer.php b/Tools/api_cache_writer.php new file mode 100644 index 0000000..fbc0145 --- /dev/null +++ b/Tools/api_cache_writer.php @@ -0,0 +1,56 @@ + vv_system_info(), + 'fallback' => vv_fallback_state(), + 'fallback_active' => vv_fallback_active(), + 'partner' => vv_partner_state(), + 'resources' => vv_system_resources(), + 'cpu' => vv_cpu_per_core(), + 'mem' => vv_memory_breakdown(), + 'net' => vv_network_stats(), + 'gpu' => vv_gpu_stats(), + 'gpu_procs' => vv_gpu_processes(), + 'containers' => vv_docker_containers(), + 'stopped' => vv_docker_stopped(), + 'transcode' => vv_transcode_sessions(), + 'ups' => vv_ups_stats(), + 'parity' => vv_parity_status(), + 'storage' => vv_storage_pools(), + 'array_disks' => vv_array_disks(), + 'disk_io' => vv_disk_io_rates(), + 'watchdog' => vv_watchdog_summary(), + 'scripts' => vv_scripts_status(), + 'rsync' => vv_rsync_status(), + 'thresholds' => vv_disk_thresholds(), + 'vms' => vv_get_vms(), + 'docker_folders' => vv_get_docker_folders(), + 'remote_hosts' => vv_remote_hosts_stats(), + '_api_status' => vv_api_get_status(), + 'ts' => time(), +]; +vv_cache_write('monitor', $monitor); + +// ── Arrs payload ────────────────────────────────────────────────────────────── +$arrs = vv_arrs_all(); +vv_cache_write('arrs', $arrs); + +$elapsed = round((microtime(true) - $t) * 1000); +echo "Cache written in {$elapsed}ms — monitor + arrs\n"; diff --git a/Tools/api_cache_writer.sh b/Tools/api_cache_writer.sh new file mode 100755 index 0000000..86e39f5 --- /dev/null +++ b/Tools/api_cache_writer.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# ============================================================================================== +# ================================= API Cache Writer =========================================== +# ============================================================================================== +# +# PURPOSE +# ───────────────────────────────────────────────────────────────────────────── +# Builds the monitor and arrs API payloads and writes them to /tmp/vv_cache/ +# so page loads can serve from the file instantly instead of making live HTTP +# calls on every request. +# +# Runs every minute via the Varaverk scheduler. /tmp is tmpfs — files are +# RAM-speed reads and auto-cleared on reboot. +# +# ============================================================================================== + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +php "$SCRIPT_DIR/api_cache_writer.php"