feat(poc): add Redis-backed widget prefetch cache

POC demonstrating Approach 7 from APPROACHES.md. This commit can be
cleanly reverted with `git revert` if the approach is not adopted.

Changes:
- packages/redis/src/index.ts: Add createWidgetPrefetchCache() factory
  that creates a cache channel per widget kind (5min TTL)
- packages/widgets/src/prefetch.ts: Check Redis cache before database
  queries. On cache miss, falls back to original DB path and writes
  through to Redis for the next request.

Expected behavior:
- First page load: cache miss → DB query → write to Redis (same as before)
- Subsequent loads within 5min: cache hit → skip DB queries entirely
- Redis unavailable: falls back to original behavior (no regression)
- Cache invalidation: automatic via 5-minute TTL

Co-authored-by: ajnart <49837342+ajnart@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]andajnart committed 2026-03-15 15:26:04 +00:00
1 parent 38538a94f9
commit deb9916d2d
2 files changed
+61 -1

No files matched your search

+12 -1
View File
@@ -1,6 +1,6 @@
import type { LogLevel } from "@homarr/core/infrastructure/logs/constants";
import { createListChannel, createQueueChannel, createSubPubChannel } from "./lib/channel";
import { createCacheChannel, createListChannel, createQueueChannel, createSubPubChannel } from "./lib/channel";
export {
createCacheChannel,
@@ -40,3 +40,14 @@ export interface LoggerMessage {
}
export const loggingChannel = createSubPubChannel<LoggerMessage>("logging");
/**
* POC: Redis cache for widget prefetch data.
* Caches serialized query data per widget kind to avoid database queries on every page load.
* Default TTL: 5 minutes (matches existing cron job intervals).
*
* Usage: see packages/widgets/src/prefetch.ts
* This can be reverted by reverting this commit.
*/
export const createWidgetPrefetchCache = (widgetKind: string) =>
createCacheChannel<Record<string, unknown>>(`widget-prefetch:${widgetKind}`, 5 * 60 * 1000);