load method

Future<Uint8List?> load(
  1. String uid, {
  2. CacheRule? rule,
  3. bool? force,
})

Load the cache image from DiskCache, you can use force to skip max age check.

Implementation

Future<Uint8List?> load(String uid, {CacheRule? rule, bool? force}) async {
  if (_metadata == null) await _initMetaData();
  force ??= false;

  try {
    if (_metadata!.containsKey(uid)) {
      if (!File(_metadata![uid]['path']).existsSync()) {
        _metadata!.remove(uid);
        await _commitMetaData();
        return null;
      }
      if (DateTime.fromMillisecondsSinceEpoch(
            _metadata![uid]['createdTime'] +
                (rule != null
                    ? rule.maxAge.inMilliseconds
                    : _metadata![uid]['maxAge']),
          ).isBefore(DateTime.now()) &&
          !force) {
        await File(_metadata![uid]['path']).delete();
        _metadata!.remove(uid);
        await _commitMetaData();
        return null;
      }
      Uint8List data = await File(_metadata![uid]['path']).readAsBytes();
      if (_metadata![uid]['crc32'] != null &&
          _metadata![uid]['crc32'] != crc32(data)) {
        await File(_metadata![uid]['path']).delete();
        _metadata!.remove(uid);
        await _commitMetaData();
        return null;
      }
      if (currentEntries >= maxEntries || _currentSizeBytes >= maxSizeBytes) {
        _metadata![uid] = _metadata!.remove(uid);
        await _commitMetaData();
      }
      return data;
    } else {
      return null;
    }
  } catch (e) {
    if (printError) print(e);
    return null;
  }
}