Skip to content

Issue 7: Offline-First Local Cache Management Engine Using IndexedDB #7

Description

@elizabetheonoja-art

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

  1. Use idb (IndexedDB wrapper) to persist cache entries with keys, values, timestamps, and TTL.
  2. Implement cacheGet<T>(key), cacheSet<T>(key, value, ttlMs), cacheDelete(key), cacheClear().
  3. Add cacheKeys(prefix?) for namespaced iteration.
  4. Add cacheGetBulk<T>(keys) returning a Map<string, T>.
  5. Expose buildCacheKey(parts: string[]) to create hierarchical keys like utility:grid:node-42:metrics.
  6. When the network is offline (navigator.onLine === false), queue write mutations in a separate offline-queue store with { key, value, timestamp, retryCount }.
  7. 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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions