pub async fn fetch_with_cache<T>(
offline: bool,
load_cache: impl FnOnce() -> Result<Option<CachedData<T>>>,
save_cache: impl FnOnce(&T) -> Result<()>,
fetch_api: impl Future<Output = Result<T>>,
) -> Result<Option<T>>Expand description
Fetch data with cache fallback.
offline— if true, return cached data only (regardless of staleness)load_cache— loads cached data (returnsOk(None)if absent)save_cache— saves fetched data to cachefetch_api— async API fetch
Returns:
- Offline + cached →
Ok(Some(data)) - Offline + no cache →
Ok(None) - Cache fresh →
Ok(Some(data)) - API success → saves +
Ok(Some(data)) - API error + stale cache →
Ok(Some(stale_data)) - API error + no cache →
Err