put method

  1. @override
Future<String> put(
  1. PrefixKey key,
  2. Future<void> write(
    1. String tempPath
    )
)
override

Calls write with a temporary path to fill, then moves the file into place atomically and evicts old states; returns the state's path. When write throws, the temporary file is removed and the error rethrown.

Implementation

@override
Future<String> put(PrefixKey key, Future<void> Function(String tempPath) write) async {
  puts.add(key.id);
  final path = '$directory/prefix_${key.id}.state';
  final temp = '$path.tmp';
  try {
    await write(temp);
    File(temp).renameSync(path);
  } catch (_) {
    if (File(temp).existsSync()) File(temp).deleteSync();
    rethrow;
  }
  _paths[key.id] = path;
  return path;
}