Skip to main content

fetch_with_cache

Function fetch_with_cache 

Source
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 (returns Ok(None) if absent)
  • save_cache — saves fetched data to cache
  • fetch_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