purgeExpired method

  1. @override
Future<int> purgeExpired({
  1. DateTime? now,
})
override

Removes every expired row and returns how many were removed.

now overrides the store's clock; omitted it reads the store's own clock.

Implementation

@override
Future<int> purgeExpired({DateTime? now}) {
  final prior = _lifecycle.inflight;
  return _lifecycle.run(() async {
    await Future.wait(prior);
    if (!await _root.exists()) return 0;
    final instant = now ?? _clock.now();
    var removed = 0;
    try {
      await for (final entry in _root.list()) {
        if (entry is! File) continue;
        final name = entry.uri.pathSegments.last;
        if (!name.endsWith('.json')) continue;
        final envelope = await _readEnvelopeFile(entry);
        if (envelope == null) continue;
        if (_isExpired(envelope, instant)) {
          await _delete(entry);
          _nameIndex.remove(name);
          removed++;
        }
      }
    } on FileSystemException catch (e) {
      throw MemoryIoException('cannot purge ${_root.path}',
          path: _root.path, cause: e);
    }
    return removed;
  });
}