Weather Caching

Weather Cache Schema

Database

  • File: weather_cache.db
  • Engine: expo-sqlite
  • Journal mode: WAL

Table: forecast_points

ColumnTypeNullNotes
cell_idTEXTNOT NULLSpatial cache bucket key from lat/lon.
forecast_timeTEXTNOT NULLCanonical UTC hour key (toHourKey(...)).
valid_toINTEGERNOT NULLExpiration timestamp in epoch milliseconds.
payloadTEXTNOT NULLJSON-serialized WeatherPoint.
last_accessedINTEGERNOT NULLEpoch milliseconds for LRU eviction.

Primary key:

  • PRIMARY KEY (cell_id, forecast_time)

Data Invariants

  • forecast_time is normalized by toHourKey(...) for both reads and writes.
  • payload must decode to WeatherPoint (time, temp, rh, wspd, sky).
  • Stale rows are rows where Date.now() >= valid_to.

Access Patterns

  • Exact lookup: getForecastPoint(cellId, forecastTime).
  • Same-hour candidates: getForecastPointsForHour(forecastTime).
  • Upsert writes: putForecastPoints(cellId, points, validTo).
  • LRU eviction: evictLRU(maxRows).
  • Exact-hit access update: getForecastPoint(...) updates last_accessed after payload validation.
  • Access-time touch for fallback hits: touchForecastPointAccess(cellId, forecastTime).

Cleanup Behavior

  • Malformed payload rows are removed on read attempts (best effort).
  • Nearby-fallback access-time touch failures do not block returning usable weather.