get method

  1. @override
Future get(
  1. String key,
  2. {Duration? expiryDuration}
)
override

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

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

Implementation

@override
Future<dynamic> get(String key, {Duration? expiryDuration}) {
  // Gets the entry from the storage
  return _getStorageEntry(key).then((entry) {
    final now = clock.now();
    final expired = entry != null && entry.isExpired(now);
    // Does this entry exists or is expired ?
    if (entry == null || expired) {
      // If expired we need to remove the value from the storage
      final pre = expired
          ? _removeStorageEntry(key, ExpiredEntryEvent(this, entry!))
          : Future.value();

      // Invoke the cache loader
      return pre.then((_) => cacheLoader(key)).then((value) {
        // If the value obtained is `null` just return it
        if (value == null) {
          return null;
        }

        return _putEntry(key, value, now, expiryDuration).then((v) => value);
      });
    } else {
      return _getEntryValue(entry, now, expiryDuration);
    }
  });
}