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
TKeyCache key type.
TValueCache 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
comparerIEqualityComparer<TKey>
Methods
Clear()
Clears all cached entries.
public void Clear()
Remove(TKey)
Removes a cached key if present.
public void Remove(TKey key)
Parameters
keyTKeyThe 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
nowSecondsdoubleCurrent monotonic time in seconds.
ttlTimeSpanMaximum age before an entry is swept.
maxScanintMaximum number of entries to inspect per sweep.
maxRemoveintMaximum 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
keyTKeyThe cache key to look up.
nowSecondsdoubleCurrent monotonic time in seconds; becomes the entry's last-seen time.
valueTValueThe cached value if found; otherwise,
default.
Returns
- bool
trueif 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
keyTKeyThe cache key.
valueTValueThe value to store.
nowSecondsdoubleCurrent monotonic time in seconds, used as the last-seen time.