removeCacheByUrl method

Future<void> removeCacheByUrl(
  1. String url, {
  2. bool singleFile = false,
})

Removes cache entries (both in-memory and disk) associated with a URL.

If singleFile is true, only the file matching the URL is removed. Otherwise, all files in the directory related to the URL are removed.

Implementation

Future<void> removeCacheByUrl(String url, {bool singleFile = false}) async {
  String key = url.generateMd5;
  await _storageInit();
  if (singleFile) {
    // delete single file from storage and memory
    await _storageCache.remove(key);
    await _memoryCache.remove(key);
  } else {
    // delete all files in directory related to the URL from storage and memory
    Directory cacheDir = Directory(await FileExt.createCachePath());
    if (!(await cacheDir.exists())) return;
    for (FileSystemEntity file in cacheDir.listSync()) {
      FileStat stat = await file.stat();
      if (stat.type == FileSystemEntityType.directory) {
        String directoryName = basename(file.path);
        if (directoryName == key) {
          Directory directory = file as Directory;
          await directory.list(recursive: true).forEach((subFile) async {
            if (subFile is File) {
              String subKey = basenameWithoutExtension(subFile.path);
              await _memoryCache.remove(subKey);
            }
          });
          await file.delete(recursive: true);
          await directory.delete(recursive: true);
        }
      }
    }
  }
}