put method

  1. @override
Future<FileSystemEntity?> put(
  1. String key,
  2. FileSystemEntity value
)
override

Inserts or updates the file for the given key in the cache.

Returns the previous file if it existed, or null otherwise. Updates the cache size and triggers eviction if necessary. The operation is thread-safe.

Implementation

@override
Future<FileSystemEntity?> put(String key, FileSystemEntity value) async {
  assert(key.isNotEmpty, 'key must not be empty');
  return await lock.synchronized(() async {
    putCount++;
    final int valueSize = await _safeSize(value);
    final FileSystemEntity? previous = map[key];
    if (previous != null) {
      // Replace the bookkeeping first; deleting the same path here would
      // remove the new file when callers overwrite an existing key in place.
      await _removeEntryLocked(key, deleteFile: false);
    }

    size += valueSize;
    _entrySizes[key] = valueSize;
    map[key] = value;

    await _trimToSizeLocked(maxSize);
    return previous;
  });
}