Add write-through cache for per-item track/episode data

lidarr_cleanup.sh and sonarr_cleanup.sh already walk every artist/series
individually (trackFile/episodefile) for their own cleanup decisions --
that walk now also writes the raw per-item data through to a short-lived
tmpfs cache (arr_item_cache_write, 4h freshness, no persistent backup
since every consumer already has its own live fallback). lidarr_missing_art.sh
runs later in the same nightly window and now reads that cache first,
skipping its own redundant per-artist walk entirely on a hit. Sonarr side
is write-through only for now -- no second consumer exists yet, but the
data's there for whenever one does. Future consumers: arr_get_cached_items()
first, live per-item fetch as fallback, same pattern as these two.
This commit is contained in:
Gmer4Lfe
2026-07-17 00:55:14 -04:00
parent a018245f40
commit de2879bdee
4 changed files with 108 additions and 6 deletions
+20 -6
View File
@@ -261,15 +261,29 @@ _artist_list=$(arr_get_tracked_data "lidarr" "$LIDARR_URL" "$LIDARR_API_KEY" "v1
_map_artist_count=$(echo "$_artist_list" | jq '. | length')
info "Fetching track files for $_map_artist_count artists..."
while IFS= read -r _artist_id; do
[[ -z "$_artist_id" ]] && continue
# Cache-first for the per-track data too — lidarr_cleanup.sh (runs earlier in the same nightly
# window) already does this exact per-artist walk for its own cleanup decisions and writes the
# result through via arr_item_cache_write(). Read that instead of repeating the walk; fall
# back to the live per-artist walk below only if it's missing or from outside this window.
# See arr_item_cache_write()/arr_get_cached_items() in common.sh. (2026-07-17)
_cached_tracks=$(arr_get_cached_items "lidarr")
if [[ -n "$_cached_tracks" ]]; then
info "Using cached track data from lidarr_cleanup.sh — skipping live per-artist walk"
while IFS=$'\t' read -r _album_id _track_path; do
[[ -z "$_album_id" || -z "$_track_path" || "$_track_path" == "null" ]] && continue
ALBUM_DIR_MAP["$_album_id"]=$(dirname "$_track_path")
done < <(curl_json "$LIDARR_URL/api/v1/trackFile?artistId=${_artist_id}&apikey=$LIDARR_API_KEY" | \
jq -r '.[] | [(.albumId | tostring), .path] | @tsv' 2>/dev/null)
done < <(echo "$_artist_list" | jq -r '.[].id')
unset _artist_list _map_artist_count _artist_id _album_id _track_path
done < <(echo "$_cached_tracks" | jq -r '.[] | [(.albumId | tostring), .path] | @tsv' 2>/dev/null)
else
while IFS= read -r _artist_id; do
[[ -z "$_artist_id" ]] && continue
while IFS=$'\t' read -r _album_id _track_path; do
[[ -z "$_album_id" || -z "$_track_path" || "$_track_path" == "null" ]] && continue
ALBUM_DIR_MAP["$_album_id"]=$(dirname "$_track_path")
done < <(curl_json "$LIDARR_URL/api/v1/trackFile?artistId=${_artist_id}&apikey=$LIDARR_API_KEY" | \
jq -r '.[] | [(.albumId | tostring), .path] | @tsv' 2>/dev/null)
done < <(echo "$_artist_list" | jq -r '.[].id')
fi
unset _artist_list _map_artist_count _artist_id _album_id _track_path _cached_tracks
info "Mapped ${#ALBUM_DIR_MAP[@]} albums with local tracks"