Scope
File: src/services/cache.ts
Problem
Field engineers operating utility meters often work in areas with intermittent or no cellular connectivity. The current cache layer (in-memory Map) loses all data on tab close, and there is no background sync mechanism to queue mutations made offline.
Requirements
- Use
idb (IndexedDB wrapper) to persist cache entries with keys, values, timestamps, and TTL.
- Implement
cacheGet<T>(key), cacheSet<T>(key, value, ttlMs), cacheDelete(key), cacheClear().
- Add
cacheKeys(prefix?) for namespaced iteration.
- Add
cacheGetBulk<T>(keys) returning a Map<string, T>.
- Expose
buildCacheKey(parts: string[]) to create hierarchical keys like utility:grid:node-42:metrics.
- When the network is offline (
navigator.onLine === false), queue write mutations in a separate offline-queue store with { key, value, timestamp, retryCount }.
- On
window.online event, replay the offline queue in FIFO order with exponential backoff on failure.
Resolution Strategy
- Open DB with
openDB(DB_NAME, DB_VERSION, { upgrade }) in a singleton.
- Create
kv (key-value) and offline-queue object stores.
- Use
requestAnimationFrame batching for bulk writes.
- Add
navigator.onLine detection via useEffect in a useOnlineStatus hook.
Tags
services, offline, cache, indexeddb
Scope
File:
src/services/cache.tsProblem
Field engineers operating utility meters often work in areas with intermittent or no cellular connectivity. The current cache layer (in-memory Map) loses all data on tab close, and there is no background sync mechanism to queue mutations made offline.
Requirements
idb(IndexedDB wrapper) to persist cache entries with keys, values, timestamps, and TTL.cacheGet<T>(key),cacheSet<T>(key, value, ttlMs),cacheDelete(key),cacheClear().cacheKeys(prefix?)for namespaced iteration.cacheGetBulk<T>(keys)returning aMap<string, T>.buildCacheKey(parts: string[])to create hierarchical keys likeutility:grid:node-42:metrics.navigator.onLine === false), queue write mutations in a separateoffline-queuestore with{ key, value, timestamp, retryCount }.window.onlineevent, replay the offline queue in FIFO order with exponential backoff on failure.Resolution Strategy
openDB(DB_NAME, DB_VERSION, { upgrade })in a singleton.kv(key-value) andoffline-queueobject stores.requestAnimationFramebatching for bulk writes.navigator.onLinedetection viauseEffectin auseOnlineStatushook.Tags
services, offline, cache, indexeddb