Class LastSeenCacheTracker<TKey, TValue>
- Namespace
- FishMMO.Server.Core.Collections
- Assembly
- FishMMO.Server.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
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
keyTKey
SweepExpired(DateTime, TimeSpan, int, int)
Sweeps entries whose last-seen exceeds the provided TTL.
public int SweepExpired(DateTime nowUtc, TimeSpan ttl, int maxScan, int maxRemove)
Parameters
Returns
SweepExpired(double, TimeSpan, int, int)
SweepExpired(DateTime, TimeSpan, int, int) for a cache timed on MonotonicClock.
public int SweepExpired(double nowSeconds, TimeSpan ttl, int maxScan, int maxRemove)
Parameters
nowSecondsdoubleCurrent NowSeconds reading.
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, DateTime, out TValue)
Tries to get a cached value and refreshes its last-seen timestamp.
public bool TryGetAndTouch(TKey key, DateTime nowUtc, out TValue value)
Parameters
keyTKeynowUtcDateTimevalueTValue
Returns
TryGetAndTouch(TKey, double, out TValue)
TryGetAndTouch(TKey, DateTime, out TValue) for a cache timed on MonotonicClock.
public bool TryGetAndTouch(TKey key, double nowSeconds, out TValue value)
Parameters
keyTKeyThe cache key to look up.
nowSecondsdoubleCurrent NowSeconds reading.
valueTValueThe cached value if found; otherwise,
default.
Returns
- bool
trueif the key was found; otherwise,false.
Remarks
A TTL is a duration. On DateTime.UtcNow a host clock stepped back kept every entry
fresh for the size of the step and one stepped forward expired the whole cache, and the
head-first sweep assumes last-seen stamps arrive in order, which a stepped clock breaks.
One cache, one clock: see FishMMO.Server.Core.Collections.MonotonicInstant.
Upsert(TKey, TValue, DateTime)
Inserts or updates a cached value and sets its last-seen timestamp.
public void Upsert(TKey key, TValue value, DateTime nowUtc)
Parameters
keyTKeyvalueTValuenowUtcDateTime
Upsert(TKey, TValue, double)
Upsert(TKey, TValue, DateTime) for a cache timed on MonotonicClock.
public void Upsert(TKey key, TValue value, double nowSeconds)
Parameters
keyTKeyThe cache key.
valueTValueThe value to store.
nowSecondsdoubleCurrent NowSeconds reading.