fetch method

Future<CacheResponse> fetch(
  1. String url, {
  2. Options options = const Options(),
})

Implementation

Future<CacheResponse> fetch(
  String url, {
  Options options = const Options(),
}) async {
  _checkClose();
  final key = _genKey(url);
  final snapshot = await _cache.get(key).catchError((e) {
    // Give up because the cache cannot be read.
    return null;
  });
  if (snapshot != null) {
    try {
      return CacheResponse.fromCache(snapshot);
    } catch (e) {
      await snapshot.close().catchError((_) {});
    }
  }
  final resource = await _pool.request();
  try {
    final response = await _fetcher.fetch(url, options: options);
    final editor = await _cache.edit(key);
    if (editor == null) {
      return response;
    } else {
      return CacheResponse.fromResponse(response, editor);
    }
  } finally {
    resource.release();
  }
}