Implement StatsCacheManager with 15-minute TTL
epic-coordinator-statistics-dashboard-foundation-task-004 — Build the StatsCacheManager that stores query results in memory with a 15-minute TTL to prevent repeated heavy queries during a coordinator's normal work session. Implement cache key generation from org_id + coordinator_id + period + activity_type parameters, TTL expiration logic, and explicit invalidation on mutation events. Use a LRU eviction strategy for memory bounding.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Implement using a LinkedHashMap
Provide a Riverpod Provider
Testing Requirements
Unit tests using flutter_test covering: (1) TTL expiration — entry inserted at T returns value at T+14m, returns null at T+15m; (2) LRU eviction — inserting N+1 entries evicts the least-recently-used; (3) deterministic key generation — identical params in different order produce the same key; (4) invalidateByPrefix removes only matching entries; (5) cache cleared on sign-out; (6) concurrent access simulation with Future.wait to verify no race conditions. Target 100% branch coverage on all cache decision paths.
Pre-aggregated Supabase views may still be slow for orgs with very large activity datasets (NHF with 1,400 chapters). If the view query plan performs sequential scans, dashboard load times could exceed acceptable thresholds and degrade the perceived value of the feature.
Mitigation & Contingency
Mitigation: Design views with composite indexes on (org_id, coordinator_id, month) from the start. Run EXPLAIN ANALYZE during development against a seeded dataset of realistic scale. Add materialized view refresh strategy if needed.
Contingency: If live view performance is insufficient, convert to materialized views refreshed on a schedule or on activity-write triggers. Expose the refresh delay transparently in the UI with a 'last updated' timestamp.
Supabase RLS policies for the stats views may not be configured correctly during initial migration, potentially allowing cross-coordinator data leakage before the RoleAccessValidator layer is reached. This is a security and compliance risk.
Mitigation & Contingency
Mitigation: Write RLS integration tests as part of this epic that explicitly verify a coordinator JWT cannot read another coordinator's stats rows. Apply RLS policies in the migration script itself, not as a manual step.
Contingency: If an RLS gap is discovered post-deployment, immediately disable the stats screen via a feature flag, apply the corrected RLS migration, and re-enable after verification. Log and audit all queries that ran during the gap window.
Cache invalidation logic may not be triggered correctly when a new activity is registered by a peer mentor or when an expense approval is granted. Stale data could cause coordinators to make decisions based on outdated KPIs, undermining trust in the dashboard.
Mitigation & Contingency
Mitigation: Define explicit invalidation event contracts with the activity registration and expense approval pipelines. Implement an event bus subscription within StatsCacheManager. Document the invalidation contract in code.
Contingency: If event-driven invalidation proves unreliable, add a manual 'Refresh' pull-to-refresh gesture on the dashboard and reduce TTL to 5 minutes as a fallback degradation strategy.