evict method

Future<Set<String>> evict({
  1. Duration? olderThan,
  2. int? maxEntries,
  3. bool where(
    1. String key
    )?,
  4. bool keepLive = false,
})

Deletes stale state, with its version key, and returns a new set of the state keys it deleted. A key's use time is when its store last hydrated from it or wrote it (kept as the file's modification time).

  • olderThan drops every key not used for that long.
  • maxEntries then keeps only that many keys, the most recently used.
  • where limits both to the keys it accepts; other keys are never evicted and do not count towards maxEntries.
  • keepLive spares, like where, the keys of live stores.

Live stores of evicted keys stop persisting.

Implementation

Future<Set<String>> evict({
  Duration? olderThan,
  int? maxEntries,
  bool Function(String key)? where,
  bool keepLive = false,
}) async {
  checkEvictArguments(olderThan: olderThan, maxEntries: maxEntries);
  // Under the lock, so use times queued by [markUsed] land first.
  final doomed = await _locked('evict', () async {
    // [keys] leaves out the keys a queued delete hides, as
    // `HydratedStorage.evict` does: they are going anyway, and must not
    // take a place [maxEntries] keeps.
    final accepted = evictionCandidates(keys, where);
    await _loadUseTimes(accepted);
    return evictionsAmong(
      accepted,
      usedAt: _usedAtOf,
      now: _clock().millisecondsSinceEpoch,
      olderThan: olderThan,
      maxEntries: maxEntries,
      live: keepLive ? () => HydratedMobX.liveTokens(storage: this) : null,
      // A delete may have been queued since the candidates were listed,
      // by [where] or while the use times loaded.
      gone: _pending.hides,
    );
  });
  if (doomed.isEmpty) return doomed;
  await HydratedMobX.clearAll(storage: this, where: doomed.contains);
  return doomed;
}