put method

  1. @override
Future<void> put(
  1. String key,
  2. dynamic value, {
  3. Duration? expiryDuration,
})
override

Add / Replace the cache value for the specified key. If specified expiryDuration is used instead of the configured expiry policy duration

  • key: the key
  • value: the value
  • expiryDuration: Expiry duration to be used in place of the configured expiry policy duration

Implementation

@override
Future<void> put(String key, dynamic value, {Duration? expiryDuration}) {
  // Try to get the entry from the cache
  return _getStorageEntry(key).then((entry) {
    final now = clock.now();
    var expired = entry != null && entry.isExpired(now);

    // If the entry does not exist or is expired
    if (entry == null || expired) {
      // If expired we remove it
      final pre = expired
          ? _removeStorageEntry(key, ExpiredEntryEvent(this, entry!))
          : Future.value();

      // And finally we add it to the cache
      return pre
          .then((_) => _putEntry(key, value, now, expiryDuration))
          .then((_) => null);
    } else {
      // Already present let's update the cache instead
      return _updateEntry(entry, value, now, expiryDuration)
          .then((_) => null);
    }
  });
}