Table of Contents

Class LastSeenCacheTracker<TKey, TValue>

Namespace
FishMMO.Auth.Core.Collections
Assembly
FishMMO-ServerAuth.dll

Queue/index tracker for key-value caches whose entries expire by last-seen timestamp. Optimized for high-frequency touch and bounded head-first TTL sweeps.

public sealed class LastSeenCacheTracker<TKey, TValue>

Type Parameters

TKey

Cache key type.

TValue

Cache value type.

Inheritance
LastSeenCacheTracker<TKey, TValue>
Inherited Members

Remarks

Time is monotonic seconds — a MonotonicClock.NowSeconds reading from the authenticator's clock. A TTL is a duration: on the wall clock a step backwards kept every entry fresh for the size of the step, and a step forward expired the whole cache at once. The head-first sweep also depends on last-seen stamps arriving in order, which a stepped wall clock breaks and a monotonic one does not. See ExpiringKeyTracker<TKey> for why there is no DateTime overload.

Constructors

LastSeenCacheTracker(IEqualityComparer<TKey>?)

Initializes a new tracker with an optional key comparer.

public LastSeenCacheTracker(IEqualityComparer<TKey>? comparer = null)

Parameters

comparer IEqualityComparer<TKey>

Methods

Clear()

Clears all cached entries.

public void Clear()

Remove(TKey)

Removes a cached key if present.

public void Remove(TKey key)

Parameters

key TKey

The cache key to remove.

SweepExpired(double, TimeSpan, int, int)

Sweeps entries whose last-seen exceeds the provided TTL.

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

Parameters

nowSeconds double

Current monotonic time in seconds.

ttl TimeSpan

Maximum age before an entry is swept.

maxScan int

Maximum number of entries to inspect per sweep.

maxRemove int

Maximum number of entries to remove per sweep.

Returns

int

Number of entries removed.

TryGetAndTouch(TKey, double, out TValue)

Tries to get a cached value and refreshes its last-seen timestamp.

public bool TryGetAndTouch(TKey key, double nowSeconds, out TValue value)

Parameters

key TKey

The cache key to look up.

nowSeconds double

Current monotonic time in seconds; becomes the entry's last-seen time.

value TValue

The cached value if found; otherwise, default.

Returns

bool

true if the key was found; otherwise, false.

Upsert(TKey, TValue, double)

Inserts or updates a cached value and sets its last-seen timestamp.

public void Upsert(TKey key, TValue value, double nowSeconds)

Parameters

key TKey

The cache key.

value TValue

The value to store.

nowSeconds double

Current monotonic time in seconds, used as the last-seen time.