prefetch<T> method
Prefetch data and store it in the cache if it's not already fresh.
Implementation
Future<T?> 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 getCachedData<T>(normalizedKey);
}
ZenQuery.activeFetches.value++;
try {
final data = await deduplicateFetch(normalizedKey, fetcher);
// Determine cache time
final query = _queries[normalizedKey];
final effectiveCacheTime = cacheTime ??
query?.config.cacheTime ??
const Duration(minutes: 5); // coverage:ignore-line
_setCacheEntry(normalizedKey, data, DateTime.now(), effectiveCacheTime);
return data;
} catch (e) {
ZenLogger.logWarning('Prefetch failed for $normalizedKey: $e');
return null;
} finally {
ZenQuery.activeFetches.value--;
}
}