add method
Adds the provided data
to the cache.
Implementation
FutureOr<File?> add(Uint8List data, [String? checksum, String? url]) {
// Set [url] to the [FIFOCache] as well, yet if [checksum] isn't specified,
// then there will be no [File] written, only this.
final String? key = checksum ?? url;
if (key != null && !FIFOCache.exists(key)) {
FIFOCache.set(key, data);
}
// Calculating SHA-256 hash from [data] on Web freezes the application.
if (!PlatformUtils.isWeb) {
checksum ??= sha256.convert(data).toString();
}
return _mutex.protect(() async {
final Directory? cache = await PlatformUtils.cacheDirectory;
if (cache != null) {
final File file = File('${cache.path}/$checksum');
if (!(await file.exists())) {
await file.writeAsBytes(data);
info.value.size = info.value.size + data.length;
info.value.modified = (await cache.stat()).modified;
info.refresh();
hashes.add('$checksum');
await _cacheLocal?.upsert(info.value);
await _cacheLocal?.register([checksum!]);
_optimizeCache();
}
return file;
}
return null;
});
}