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:
@@ -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());
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
// API cache writer — runs every minute via Varaverk scheduler.
|
||||
// Builds monitor + arrs payloads and writes them to /tmp/vv_cache/ so page
|
||||
// loads can serve instantly from the file instead of making live HTTP calls.
|
||||
//
|
||||
// Called by api_cache_writer.sh (bash wrapper required by the scheduler).
|
||||
|
||||
$_base = dirname(__DIR__) . '/Plugin/unraid';
|
||||
require_once $_base . '/include/monitor.php';
|
||||
require_once $_base . '/include/vms.php';
|
||||
require_once $_base . '/include/docker_folders.php';
|
||||
require_once $_base . '/include/arrs.php';
|
||||
|
||||
$t = microtime(true);
|
||||
|
||||
// ── Monitor payload ───────────────────────────────────────────────────────────
|
||||
// Call vv_api_data() once — result is static-cached for the rest of this process.
|
||||
vv_api_data();
|
||||
|
||||
$monitor = [
|
||||
'system' => 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";
|
||||
Executable
+18
@@ -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"
|
||||
Reference in New Issue
Block a user