hydrate<T> method

Future<T?> hydrate<T>(
  1. String key,
  2. ZenQueryConfig<T> config
)

Try to hydrate a query from storage

Implementation

Future<T?> hydrate<T>(
  String key,
  ZenQueryConfig<T> config,
) async {
  final storage = config.storage ?? _storage;
  if (storage == null) return null;
  if (config.fromJson == null) return null;

  try {
    final entry = await storage.read(key);
    if (entry == null) return null;

    final timestamp =
        DateTime.fromMillisecondsSinceEpoch(entry['timestamp'] as int);

    // Check if expired based on cacheTime
    final cacheTime = config.cacheTime ?? const Duration(minutes: 5);
    if (DateTime.now().difference(timestamp) > cacheTime) {
      await storage.delete(key);
      return null;
    }

    final data = config.fromJson!(entry['data'] as Map<String, dynamic>);

    // Update memory cache
    updateCache(key, data, timestamp);

    ZenLogger.logDebug('Hydrated query: $key');
    return data;
  } catch (e, stack) {
    ZenLogger.logError('Failed to hydrate query $key', e, stack);
    return null;
  }
}