get method

  1. @override
Future<T?> get(
  1. String key, {
  2. VaultEntryDelegate<T>? delegate,
})
override

Returns the vault value for the specified key.

  • key: the key
  • delegate: provides the caller a way of changing the VaultEntry before persistence

Implementation

@override
Future<T?> get(String key, {VaultEntryDelegate<T>? delegate}) {
  // Current time
  final now = clock.now();
  // #region Statistics
  Stopwatch? watch;
  Future<T?> Function(T? value) posGet = (T? value) => Future.value(value);
  if (statsEnabled) {
    watch = clock.stopwatch()..start();
    posGet = (T? value) {
      stats.increaseGets();
      if (watch != null) {
        stats.addGetTime(watch.elapsedMicroseconds);
        watch.stop();
      }

      return Future.value(value);
    };
  }
  // #endregion

  // Gets the entry from the storage
  return _getStorageEntry(key).then((entry) {
    // Does this entry exists ?
    if (entry == null) {
      return vaultLoader(key).then((value) {
        // If the value obtained is `null` just return it
        if (value == null) {
          return Future<T?>.value();
        }

        return _newEntry(_entryBuilder(key, value, now, delegate: delegate))
            .then((_) => value);
      });
    } else {
      return _getEntryValue(entry, now);
    }
  }).then(posGet);
}