From 9d27fac3b1746d242a21bbdeff39f4c67530d247 Mon Sep 17 00:00:00 2001 From: Gmer4Lfe Date: Thu, 16 Jul 2026 23:44:26 -0400 Subject: [PATCH] Move arr tracked-data cache to tmpfs, keep disk copy as persistent backup Reads/writes now hit tmpfs (ARR_CACHE_DIR) instead of the array disk -- a full rebuild for all three arrs measures ~12s live, so there's no real cost to losing it on reboot. The existing on-disk file becomes a backup that arr_cache_write() keeps in sync on every write, and arr_cache_age_seconds() transparently restores it into tmpfs the moment it notices tmpfs is missing -- so a cache that was fresh before reboot reads as fresh after too, closing the cold-start gap without needing a dedicated restore step anywhere else. --- common.sh | 34 ++++++++++++++++++++++++++++------ load_config.sh | 3 ++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/common.sh b/common.sh index 0f5177b..4c2e740 100755 --- a/common.sh +++ b/common.sh @@ -2294,7 +2294,17 @@ declare -gA ARR_API_VERSION=( [radarr]="v3" ) -arr_cache_file() { echo "${DATA_DIR}/${1}_tracked_cache.json"; } +# tmpfs — primary read/write location. Cleared every reboot; that's fine, a full rebuild for +# all three arrs measures ~12s live, and arr_cache_age_seconds() transparently restores from +# the persistent backup below the moment it notices this is missing. +arr_cache_file() { echo "${ARR_CACHE_DIR}/${1}_tracked_cache.json"; } + +# Persistent backup — survives reboot. Kept in sync by arr_cache_write() dual-writing here on +# every write, so it's never more than one write-cycle stale (2026-07-17). Exists purely so a +# reboot doesn't leave the tmpfs cache genuinely empty until the next live fetch completes — +# not a source of truth in its own right, just what tmpfs gets restored from when missing. +arr_cache_backup_file() { echo "${DATA_DIR}/${1}_tracked_cache.json"; } + arr_rescan_duration_db() { echo "${DATA_DIR}/${1}_rescan_duration.db"; } # Writes the current library-list JSON + computed total tracked count to arr_type's cache. @@ -2332,14 +2342,16 @@ arr_cache_write() { total=$(echo "$items_json" | jq "$expr" 2>/dev/null) [[ -z "$total" || "$total" == "null" ]] && return 1 - local cache_file items_tmp + local cache_file backup_file items_tmp cache_file=$(arr_cache_file "$arr_type") + backup_file=$(arr_cache_backup_file "$arr_type") items_tmp=$(mktemp) echo "$items_json" > "$items_tmp" - mkdir -p "$(dirname "$cache_file")" 2>/dev/null + mkdir -p "$(dirname "$cache_file")" "$(dirname "$backup_file")" 2>/dev/null jq -c -n --slurpfile items "$items_tmp" --argjson total "$total" --argjson ts "$(date +%s)" \ '{ts:$ts, totalTracked:$total, items:$items[0]}' > "${cache_file}.tmp" 2>/dev/null \ - && mv "${cache_file}.tmp" "$cache_file" + && mv "${cache_file}.tmp" "$cache_file" \ + && cp "$cache_file" "$backup_file" 2>/dev/null rm -f "$items_tmp" } @@ -2353,12 +2365,22 @@ arr_cache_read_raw() { jq -c '.items // empty' "$cache_file" 2>/dev/null } -# Echoes arr_type's cache age in seconds, or a very large number if no cache exists (so age -# comparisons naturally treat "no cache" the same as "very stale cache"). +# Echoes arr_type's cache age in seconds, or a very large number if no cache and no backup +# exist (so age comparisons naturally treat that as "very stale"). Transparently restores the +# tmpfs cache from its persistent backup first if tmpfs is missing (e.g. right after a reboot) +# — a cache that was fresh before reboot reads as fresh here too, not as if it never existed. arr_cache_age_seconds() { local arr_type="$1" local cache_file cache_file=$(arr_cache_file "$arr_type") + if [[ ! -f "$cache_file" ]]; then + local backup_file + backup_file=$(arr_cache_backup_file "$arr_type") + if [[ -f "$backup_file" ]]; then + mkdir -p "$(dirname "$cache_file")" 2>/dev/null + cp "$backup_file" "$cache_file" 2>/dev/null + fi + fi [[ -f "$cache_file" ]] || { echo 999999999; return; } local ts ts=$(jq -r '.ts // 0' "$cache_file" 2>/dev/null) diff --git a/load_config.sh b/load_config.sh index ab2dfaf..efe4dd6 100755 --- a/load_config.sh +++ b/load_config.sh @@ -136,7 +136,8 @@ LOG_DIR="/var/log/varaverk" VV_CACHE_DIR="/tmp/vv_cache" CONF_RAM_CACHE_DIR="/tmp/.cache/vv/d" # tmpfs — cleared every reboot, repopulated by conf_sync.sh - export CONF_DIR LOG_DIR VV_CACHE_DIR CONF_RAM_CACHE_DIR + ARR_CACHE_DIR="/tmp/arr_cache" # tmpfs — cleared every reboot, restored from DATA_DIR backup by arr_cache_age_seconds() + export CONF_DIR LOG_DIR VV_CACHE_DIR CONF_RAM_CACHE_DIR ARR_CACHE_DIR # ━━━ Cleanup ━━━ unset _conf _host_confs_loaded _adapter LOAD_CONFIG_DIR \ No newline at end of file