getAndRemove method

  1. @override
Future getAndRemove(
  1. String key
)
override

Removes the entry for a key only if currently mapped to some value.

  • key: key with which the specified value is associated

Returns the value if one existed or null if no mapping existed for this key

Implementation

@override
Future<dynamic> getAndRemove(String key) {
  // Try to get the entry from the cache
  return _getStorageEntry(key).then((entry) {
    if (entry != null) {
      // The entry exists on cache
      // Let's check if it is expired
      if (entry.isExpired(clock.now())) {
        // If expired let's remove from cache, send an expired event and return
        // null
        return _removeStorageEntry(key, ExpiredEntryEvent(this, entry))
            .then((_) => null);
      } else {
        // If not expired let's remove from cache, send a removed event and
        // return the value
        return _removeStorageEntry(key, RemovedEntryEvent(this, entry))
            .then((value) => entry.value);
      }
    }

    return Future.value();
  });
}