feat: replace server-side Redis caching with TanStack Query AsyncStoragePersister

Consolidate two overlapping cache systems (broken TanStack persister + per-query Redis hash + pub/sub) into one: TanStack Query as single source of truth with Redis as its persistence layer via createAsyncStoragePersister.

- Replace QueryClientProvider with PersistQueryClientProvider backed by Redis
- Set global query defaults (staleTime: 0, refetchInterval: 30s, gcTime: 24h)
- Remove all per-widget staleTime/refetchInterval/retry/refetchOnWindowFocus overrides
- Replace cached request handlers with simplified fetch-only versions
- Remove all tRPC subscription procedures and useSubscription calls in favor of polling
- Delete 20 cron jobs that only pre-warmed server-side cache
- Simplify Redis from per-query hash to single blob per user+board
This commit is contained in:
Thomas Camlong committed 2026-06-24 14:55:17 +02:00
1 parent 58cb65cb3a
commit 6416080b79
165 files changed
+546 -3456

No files matched your search

@@ -8,24 +8,6 @@ import type { BeszelSystemRow } from "./types";
type SystemRowWithKey = BeszelSystemRow & { _key: string };
export const useBeszelSystemsSubscription = (integrationIds: string[], enabled = true) => {
const utils = clientApi.useUtils();
clientApi.widget.beszel.subscribeSystems.useSubscription(
{ integrationIds },
{
enabled,
onData(data) {
utils.widget.beszel.getSystems.setData({ integrationIds }, (prev) => {
if (!prev) return prev;
return prev.map((r) =>
r.integrationId === data.integrationId ? { ...r, systems: data.systems, updatedAt: data.timestamp } : r,
);
});
},
},
);
};
export const useBeszelFilteredSystems = (
results: { integrationId: string; systems: BeszelSystemRow[] }[],
statusFilter: string,
@@ -43,7 +43,6 @@ export const BeszelSystemStatsModal = createModal<BeszelSystemStatsModalProps>((
timePeriod,
includeDocker: false,
},
{ staleTime: 30_000 },
);
const mappers = useMemo(
@@ -1,92 +1,26 @@
"use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useMemo } from "react";
import { clientApi } from "@homarr/api/client";
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
const MAX_BUFFER = 30;
const FLUSH_INTERVAL_MS = 3000;
interface LiveStatsData {
systemStats: BeszelSystemStatsRecord[];
containerStats: BeszelContainerStatsRecord[];
}
function appendToBuffer(
prev: LiveStatsData | null,
incoming: { stats: BeszelSystemStatsRecord; containerStats: BeszelContainerStatsRecord | null },
): LiveStatsData {
const systemStats = [...(prev?.systemStats ?? []), incoming.stats].slice(-MAX_BUFFER);
const prevContainer = prev?.containerStats ?? [];
if (!incoming.containerStats) {
return { systemStats, containerStats: prevContainer };
}
return { systemStats, containerStats: [...prevContainer, incoming.containerStats].slice(-MAX_BUFFER) };
}
export const useLiveStats = (integrationIds: string[], systemId: string, enabled: boolean) => {
const [data, setData] = useState<LiveStatsData | null>(null);
const [error, setError] = useState<Error | null>(null);
const bufferRef = useRef<LiveStatsData | null>(null);
const dirtyRef = useRef(false);
const generationRef = useRef(0);
const integrationKey = [...integrationIds].sort().join(",");
useEffect(() => {
generationRef.current += 1;
setData(null);
setError(null);
bufferRef.current = null;
dirtyRef.current = false;
}, [systemId, enabled, integrationKey]);
const onData = useCallback(
(incoming: { stats: BeszelSystemStatsRecord; containerStats: BeszelContainerStatsRecord | null }) => {
const gen = generationRef.current;
queueMicrotask(() => {
if (generationRef.current !== gen) return;
setError(null);
const isFirst = !bufferRef.current;
bufferRef.current = appendToBuffer(bufferRef.current, incoming);
dirtyRef.current = true;
if (isFirst) {
setData(bufferRef.current);
}
});
},
[],
const queryInput = useMemo(
() => ({
integrationIds,
systemId,
timePeriod: "1m" as const,
includeDocker: true,
}),
[integrationIds, systemId],
);
const onError = useCallback((err: unknown) => {
if (err instanceof Error) {
setError(err);
return;
}
setError(new Error(String(err)));
}, []);
const subscriptionInput = useMemo(() => ({ integrationIds, systemId }), [integrationKey, systemId]);
const subscriptionEnabled = enabled && systemId !== "";
useEffect(() => {
if (!subscriptionEnabled) return;
const gen = generationRef.current;
const id = setInterval(() => {
if (generationRef.current !== gen) return;
if (!dirtyRef.current || !bufferRef.current) return;
dirtyRef.current = false;
setData(bufferRef.current);
}, FLUSH_INTERVAL_MS);
return () => clearInterval(id);
}, [subscriptionEnabled, integrationKey, systemId]);
clientApi.widget.beszel.subscribeSystemStats.useSubscription(subscriptionInput, {
enabled: subscriptionEnabled,
onData,
onError,
const { data, error } = clientApi.widget.beszel.getSystemStats.useQuery(queryInput, {
enabled: enabled && systemId !== "",
});
return { data, error };
return {
data: data ? { systemStats: data.systemStats, containerStats: data.containerStats } : null,
error: error ?? (data?.error ? new Error(data.error) : null),
};
};