prefetch<T> method

Future<void> prefetch<T>({
  1. required Object queryKey,
  2. required Future<T> fetcher(),
  3. Duration? staleTime,
  4. Duration? cacheTime,
})

Prefetch data and store it in the cache if it's not already fresh.

Implementation

Future<void> prefetch<T>({
  required Object queryKey,
  required Future<T> Function() fetcher,
  Duration? staleTime,
  Duration? cacheTime,
}) async {
  final normalizedKey = QueryKey.normalize(queryKey);

  if (_isDataFresh(normalizedKey, staleTime)) return;

  try {
    final data = await deduplicateFetch(normalizedKey, fetcher);

    // Determine cache time
    final query = _queries[normalizedKey];
    final effectiveCacheTime =
        cacheTime ?? query?.config.cacheTime ?? const Duration(minutes: 5);

    _setCacheEntry(normalizedKey, data, DateTime.now(), effectiveCacheTime);
  } catch (e) {
    ZenLogger.logWarning('Prefetch failed for $normalizedKey: $e');
  }
}