peekExpired method
Up to limit keys whose expiry instant is at-or-before asOf
(default: now), in ascending-expiry order. limit: 1 answers
"which key is next to expire". limit: null means no limit —
the same set as getExpiredKeys, but with guaranteed
ascending-expiry ordering. Ties (same expiry instant) are
yielded in implementation-defined order.
Bounded counterpart to getExpiredKeys — use this for next-up probes and batched sweeps; the unbounded variant stays for the "drain everything" caller.
The returned Future completes once the backend has accepted
the request; the Stream then yields the keys (empty stream
if none match). Snapshot semantics as for nextExpiresAt.
Implementation
@override
Future<Stream<String>> peekExpired({DateTime? asOf, int? limit}) async {
final cutoff = asOf ?? DateTime.timestamp();
if (limit != null && limit <= 0) return const Stream.empty();
final hits = <MapEntry<String, DateTime>>[];
_expiryIndex.forEach((key, expiry) {
if (!expiry.isAfter(cutoff)) hits.add(MapEntry(key, expiry));
});
hits.sort((a, b) => a.value.compareTo(b.value));
final limited = limit == null ? hits : hits.take(limit);
return Stream.fromIterable(limited.map((e) => Utf7.encode(e.key)));
}