save method

Future<bool> save(
  1. String uid,
  2. Uint8List data,
  3. CacheRule rule
)

Save the cache image to DiskCache.

Implementation

Future<bool> save(String uid, Uint8List data, CacheRule rule) async {
  if (_metadata == null) await _initMetaData();
  Directory dir = Directory(join(
      (rule.storeDirectory == StoreDirectoryType.temporary
              ? await getTemporaryDirectory()
              : await getApplicationDocumentsDirectory())
          .path,
      'imagecache'));

  try {
    if (!dir.existsSync()) dir.createSync(recursive: true);
    await File(join(dir.path, uid)).writeAsBytes(data);

    Map<String, dynamic> metadata = {
      'path': join(dir.path, uid),
      'createdTime': DateTime.now().millisecondsSinceEpoch,
      'crc32': rule.checksum ? crc32(data) : null,
      'size': data.lengthInBytes,
      'maxAge': rule.maxAge.inMilliseconds,
    };
    _metadata![uid] = metadata;
    await _checkCacheSize();
    await _commitMetaData(true);

    return true;
  } catch (e) {
    if (printError) print(e);
    return false;
  }
}