Data Layer low complexity mobile
0
Dependencies
1
Dependents
1
Entities
0
Integrations

Description

In-memory and optional local-storage cache for org-specific report field schemas. Reduces Supabase round-trips when the user opens multiple reports within the same session. Implements TTL-based invalidation to pick up schema config changes.

Feature: Structured Post-Session Report

report-schema-cache

Summaries

The Report Schema Cache is a performance optimization component that makes the app feel fast and responsive even when users are opening multiple reports across the same working session. By avoiding repeated round-trips to the cloud database to fetch the same field configuration data, it reduces latency and lowers database query costs at scale. For organizations with large field teams opening many reports daily, this translates directly into a smoother user experience and reduced infrastructure spend. The time-based invalidation mechanism ensures that when administrators update report configurations, those changes are automatically picked up without requiring users to log out and back in, keeping the operational workflow uninterrupted and the data presented always reasonably current.

This is a low-complexity, mobile-only infrastructure component with no external dependencies, making it an isolated and self-contained deliverable. Development scope is well-bounded: implement an in-memory store keyed by org ID, add optional localStorage persistence for cross-session durability, and wire in TTL-based expiry logic using the `isExpired(orgId)` check before serving cached values. Testing should cover cache hits, misses, TTL expiry, manual invalidation, and the `clear()` path used on logout. The primary scheduling risk is agreeing on the correct TTL value with product stakeholders — too short reduces the caching benefit, too long means schema changes reach users slowly.

This decision should be documented as a configurable constant rather than a hardcoded value to allow easy tuning post-launch without a code change.

Report Schema Cache is a client-side caching layer for `report-field-schema` objects, keyed by `orgId`, running exclusively in the mobile execution context. The core data structure is a plain in-memory `Map`. The `isExpired(orgId)` method computes `Date.now() - entry.cachedAt > TTL_MS` and should be called inside `get(orgId)` before returning a value — return `null` on miss or expiry so the caller knows to re-fetch. For optional localStorage persistence, serialize the schema with `JSON.stringify` and store under a namespaced key like `rsc:{orgId}`; deserialize on app resume to restore the warm cache state.

The `invalidate(orgId)` method should remove both the in-memory entry and the localStorage key. Ensure `clear()` flushes both stores and is called on user logout to prevent cross-user data leakage. TTL should be a module-level constant (e.g., `const SCHEMA_TTL_MS = 5 * 60 * 1000`) for easy configuration.

Responsibilities

  • Store parsed field schemas keyed by org ID
  • Return cached schema if TTL has not expired
  • Invalidate and evict stale cache entries

Interfaces

get(orgId)
set(orgId, schema)
invalidate(orgId)
clear()
isExpired(orgId)

Relationships

Dependents (1)

Components that depend on this component

Related Data Entities (1)

Data entities managed by this component

API Contract

View full contract →
REST /api/v1/report-schema-cache 6 endpoints
GET /api/v1/report-schema-cache List all cached schemas with their TTL status
GET /api/v1/report-schema-cache/:orgId Get the cached schema for an org
POST /api/v1/report-schema-cache Manually seed a schema into the cache
PUT /api/v1/report-schema-cache/:orgId Replace the cached schema for an org
DELETE /api/v1/report-schema-cache/:orgId Invalidate (evict) the cached schema for an org
DELETE /api/v1/report-schema-cache Clear all cached schemas (full cache flush)