forget method

  1. @override
Future<void> forget(
  1. String key
)
override

Removes a value from the cache by its key.

Deletes the cached item associated with the key if it exists. If the key does not exist, this operation should be a no-op (no exception thrown).

Throws an exception if the key is null or empty.

  • Parameters:
    • key: A non-null, non-empty string representing the cache key.

Implementation

@override
Future<void> forget(String key) async {
  if (key.isEmpty) {
    throw ArgumentError('Cache key cannot be empty');
  }

  final filePath = _getFilePath(key);
  final file = File(filePath);

  if (!await file.exists()) {
    return; // Already doesn't exist
  }

  // On Windows, file handles may not be released immediately after file operations.
  // Add a small delay to allow file handles to be released and prevent access conflicts.
  await Future.delayed(const Duration(milliseconds: 50));

  try {
    await file.delete();
    _stats.deletions++;
    await _saveMetadata();
  } catch (e) {
    // If deletion fails (e.g., due to file locking), just continue
    // This can happen if the file is locked by another process
  }
}