Table of Contents

Class SingleFlightCache<TKey, TValue>

Namespace
FishMMO.Server.Core.Collections
Assembly
FishMMO.Server.dll

A thread-safe, fixed-TTL cache of asynchronous reads in which every caller asking for the same key while a read is in flight shares that one read.

public sealed class SingleFlightCache<TKey, TValue>

Type Parameters

TKey

Cache key type.

TValue

Cached value type.

Inheritance
SingleFlightCache<TKey, TValue>
Inherited Members

Remarks

TimedCache<TKey, TValue> answers "is there a fresh value"; on a miss every caller goes and reads for itself. For a value many players ask for at once — the first page of a leaderboard the moment a season ends, say — that is N identical database reads where one would do. Here the first caller on a miss starts the read and every caller behind it, until the read completes and then until its TTL runs out, is handed the same task.

Freshness is measured from when the read STARTED. A value read for 2 s then kept for a 60 s TTL is served until 60 s after it began, not 62 — the data is as old as the moment the database was asked, whatever the read cost.

Failures are never cached. A read that throws, or whose value the caller's keep predicate rejects, is removed before its task completes, so every caller sharing it sees that one failure and the next caller after starts a fresh read. A shard whose database blipped for a second must not serve "unavailable" for the next minute.

The read itself runs outside the lock; the lock only guards the table. Continuations run asynchronously, so completing a read never runs a waiting caller's code on the reading thread while it still holds anything.

Constructors

SingleFlightCache(Func<DateTime>, IEqualityComparer<TKey>)

Initializes a new cache.

public SingleFlightCache(Func<DateTime> utcNow = null, IEqualityComparer<TKey> comparer = null)

Parameters

utcNow Func<DateTime>

Clock; injected by tests. Defaults to MonotonicClock, carried in a DateTime (FishMMO.Server.Core.Collections.MonotonicInstant): a TTL is a local duration, and on the wall clock a host stepped back served one read for the size of the step. The cache only ever subtracts its clock's readings from one another.

comparer IEqualityComparer<TKey>

Optional key comparer.

Properties

Count

Entries currently held, in flight or completed.

public int Count { get; }

Property Value

int

Methods

Clear()

Drops every entry, with the same in-flight rule as Invalidate(TKey).

public void Clear()

GetOrFetchAsync(TKey, TimeSpan, Func<Task<TValue>>, Func<TValue, bool>)

Returns the cached value for key, joins a read already in flight for it, or starts one.

public Task<TValue> GetOrFetchAsync(TKey key, TimeSpan ttl, Func<Task<TValue>> fetch, Func<TValue, bool> keep = null)

Parameters

key TKey

Cache key.

ttl TimeSpan

How long a completed read is served, from when it started.

fetch Func<Task<TValue>>

The read. Called at most once per miss, outside the lock.

keep Func<TValue, bool>

Optional: whether a completed value may be cached. A rejected value is still returned to every caller that shared the read; it is simply not served to anyone after.

Returns

Task<TValue>

The value, or the read's exception.

Invalidate(TKey)

Drops the entry for one key. A read in flight still completes for the callers already sharing it; the next caller starts a new one.

public void Invalidate(TKey key)

Parameters

key TKey

SweepExpired(TimeSpan, int, int)

Removes completed entries older than ttl, oldest first, bounded.

public int SweepExpired(TimeSpan ttl, int maxScan, int maxRemove)

Parameters

ttl TimeSpan

Entries whose read started longer ago than this are removed.

maxScan int

Maximum queue nodes to inspect.

maxRemove int

Maximum entries to remove.

Returns

int

Entries removed.

Remarks

Stops at the first entry that is still fresh or still in flight: entries are queued in start order, so everything behind it started later. A read in flight is always bounded by the database command timeout, so it cannot hold the sweep up for long.