delete method

Future<void> delete(
  1. Object target
)

Removes what target names: a String key drops that entry, a DVCacheTag drops every entry tagged with it, and DVCache.all drops every entry and every tag.

await DV.Cache.delete('greeting');                   // a key
await DV.Cache.delete(const DVCacheTag('products')); // every key tagged products
await DV.Cache.delete(DVCache.all);                  // every key

The parameter is an Object because Dart has no union type: a String cannot implement the sealed DVCacheTarget, and an extension type over String is a String at runtime, so a tag spelled that way could not be told from a key. Anything that is not a String or a DVCacheTarget is an ArgumentError naming its type, and removes nothing.

Implementation

Future<void> delete(Object target) async {
  switch (target) {
    case final String key:
      await _store.delete(_scoped(key));
    case DVCacheTag(:final String name):
      final DVCacheAdapter store = _store;
      // Tags record the scoped key: this removes exactly the entries the
      // tenant that tagged them can see.
      for (final String scoped in _tags.revalidateTag(name)) {
        await store.delete(scoped);
      }
    case _DVCacheAll():
      await _store.clear();
      _tags.clear();
    default:
      throw ArgumentError.value(
        target,
        'target',
        'delete takes a String key, a DVCacheTag or DVCache.all, not a '
            '${target.runtimeType}',
      );
  }
}