keepCacheHealth method

Future<void> keepCacheHealth()

Clean up the bad cache files in metadata.

Implementation

Future<void> keepCacheHealth() async {
  if (_metadata == null) await _initMetaData();
  _metadata!.removeWhere((k, v) {
    if (!File(v['path']).existsSync()) return true;
    if (DateTime.fromMillisecondsSinceEpoch(v['createdTime'] + v['maxAge'])
        .isBefore(DateTime.now())) {
      File(v['path']).deleteSync();
      return true;
    }
    Uint8List data = File(v['path']).readAsBytesSync();
    if (v['crc32'] != null && v['crc32'] != crc32(data)) {
      File(v['path']).deleteSync();
      return true;
    }
    return false;
  });
  await _checkCacheSize();
  await _commitMetaData();
}