diff --git a/apps/web-dashboard/src/lib/api.ts b/apps/web-dashboard/src/lib/api.ts index f817e37..7a9848f 100644 --- a/apps/web-dashboard/src/lib/api.ts +++ b/apps/web-dashboard/src/lib/api.ts @@ -1,7 +1,10 @@ const API_BASE = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000"; +const CACHE_TTL = parseInt(process.env.NEXT_PUBLIC_CACHE_TTL || "30000"); let authToken: string | null = null; +import { getCachedData, invalidateCachePattern } from "./cache"; + export function setAuthToken(token: string) { authToken = token; if (typeof window !== "undefined") { @@ -22,9 +25,22 @@ export function clearAuthToken() { if (typeof window !== "undefined") { localStorage.removeItem("auth_token"); } + invalidateCachePattern(".*"); } -async function apiFetch(path: string, init?: RequestInit): Promise { +async function apiFetch(path: string, init?: RequestInit, useCache: boolean = false): Promise { + if (useCache && init?.method === "GET") { + try { + return await getCachedData(path, () => fetchUncached(path, init), CACHE_TTL); + } catch (e) { + console.error("Cache fetch failed, falling back to direct fetch:", e); + return fetchUncached(path, init); + } + } + return fetchUncached(path, init); +} + +async function fetchUncached(path: string, init?: RequestInit): Promise { const token = getAuthToken(); const headers: Record = { "Content-Type": "application/json" }; if (token) { diff --git a/apps/web-dashboard/src/lib/cache.ts b/apps/web-dashboard/src/lib/cache.ts new file mode 100644 index 0000000..b0273d9 --- /dev/null +++ b/apps/web-dashboard/src/lib/cache.ts @@ -0,0 +1,88 @@ +interface CacheEntry { + data: T; + timestamp: number; + ttl: number; +} + +class DataCache { + private cache: Map>; + private defaultTTL: number; + + constructor(defaultTTL: number = 60000) { + this.cache = new Map(); + this.defaultTTL = defaultTTL; + } + + set(key: string, data: T, ttl?: number): void { + const entry: CacheEntry = { + data, + timestamp: Date.now(), + ttl: ttl || this.defaultTTL, + }; + this.cache.set(key, entry); + } + + get(key: string): T | null { + const entry = this.cache.get(key); + if (!entry) return null; + + const now = Date.now(); + if (now - entry.timestamp > entry.ttl) { + this.cache.delete(key); + return null; + } + + return entry.data as T; + } + + invalidate(key: string): void { + this.cache.delete(key); + } + + invalidatePattern(pattern: string): void { + const regex = new RegExp(pattern); + for (const key of this.cache.keys()) { + if (regex.test(key)) { + this.cache.delete(key); + } + } + } + + clear(): void { + this.cache.clear(); + } + + size(): number { + return this.cache.size; + } +} + +export const cache = new DataCache(); + +export function getCachedData( + key: string, + fetcher: () => Promise, + ttl?: number +): Promise { + const cached = cache.get(key); + if (cached !== null) { + return Promise.resolve(cached); + } + + return fetcher().then((data) => { + cache.set(key, data, ttl); + return data; + }); +} + +export function invalidateCache(key: string): void { + cache.invalidate(key); +} + +export function invalidateCachePattern(pattern: string): void { + cache.invalidatePattern(pattern); +} + +export function clearCache(): void { + cache.clear(); +}