Skip to main content

Cache System

How It Worksโ€‹

APScheduler (60 min)
โ””โ”€โ”€ run_compute(force=False)
โ”œโ”€โ”€ fetch weather + air quality data
โ”œโ”€โ”€ build_ensemble_forecast_df(days=7)
โ”‚ โ†’ src/airsafenet_ai/data/forecast_cache.csv
โ”œโ”€โ”€ history_range_payload(days=30)
โ”‚ โ†’ src/airsafenet_ai/data/history_cache.csv
โ”œโ”€โ”€ get_ensemble_current_snapshot(profile)
โ”‚ โ†’ src/airsafenet_ai/data/current_cache.json
โ”œโ”€โ”€ compute_district_heatmap()
โ”‚ โ†’ src/airsafenet_ai/data/district_cache.csv
โ””โ”€โ”€ check_anomaly()
โ†’ src/airsafenet_ai/data/anomaly_log.json

Cache Freshnessโ€‹

The cache is considered fresh for 70 minutes after the last compute. If stale, endpoints still return the last cached data with a cache_stale: true flag.

Manual Triggerโ€‹

# Via API (requires admin key)
curl -X POST http://localhost:8000/admin/compute \
-H "X-Admin-Key: your_admin_key"

# Or via .NET backend
curl -X POST http://localhost:7276/api/admin/compute \
-H "Authorization: Bearer your_jwt_token"

Cache Filesโ€‹

FileContentUpdated
forecast_cache.csv7-day hourly forecast ร— 4 profilesEvery 60 min
history_cache.csv30-day hourly history ร— 4 profilesEvery 60 min
current_cache.jsonCurrent snapshot ร— 4 profilesEvery 60 min
district_cache.csv22-district current AQIEvery 60 min
anomaly_log.jsonSpike detection eventsPer event