put method

  1. @override
Future<void> put(
  1. String key,
  2. T value, {
  3. VaultEntryDelegate<T>? delegate,
})
override

Add / Replace the vault value for the specified key.

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

Implementation

@override
Future<void> put(String key, T value, {VaultEntryDelegate<T>? delegate}) {
  // Current time
  final now = clock.now();
  // #region Statistics
  Stopwatch? watch;
  Future<void> Function(dynamic) posPut = (_) => Future<void>.value();
  if (statsEnabled) {
    watch = clock.stopwatch()..start();
    posPut = (_) {
      stats.increasePuts();

      if (watch != null) {
        stats.addPutTime(watch.elapsedMicroseconds);
        watch.stop();
      }

      return Future<void>.value();
    };
  }
  // #endregion

  // Try to get the entry from the vault
  return _getStorageInfo(key).then((info) {
    // If the entry does not exist
    if (info == null) {
      // And finally we add it to the vault
      return _newEntry(_entryBuilder(key, value, now));
    } else {
      // Already present let's update the vault instead
      return _replaceEntry(info, value, now);
    }
  }).then(posPut);
}