set method

Future<void> set(
  1. String key,
  2. Map<String, dynamic> data, {
  3. Duration ttl = const Duration(hours: 24),
})

Writes data under key with the given ttl.

Silently ignores I/O errors (e.g. disk full, permission denied) so that cache failures are never fatal to a scan.

Implementation

Future<void> set(String key, Map<String, dynamic> data,
    {Duration ttl = const Duration(hours: 24)}) async {
  final file = _file(key);
  try {
    await file.parent.create(recursive: true);
    await file.writeAsString(
      json.encode({
        '_exp': DateTime.now().add(ttl).millisecondsSinceEpoch,
        '_v': _schemaVersion,
        '_data': data,
      }),
      flush: true,
    );
  } catch (_) {
    // disk full, permissions — not fatal
  }
}