Class TimedCache<TKey, TValue>
- Namespace
- FishMMO.Server.Core.Collections
- Assembly
- FishMMO.Server.dll
A thread-safe, write-through TTL cache where entries expire after a fixed duration from when they were stored. Reads do NOT extend the lifetime (unlike LastSeenCacheTracker<TKey, TValue>).
Supports bounded head-first SweepExpired(DateTime, TimeSpan, int, int) for memory management.
public sealed class TimedCache<TKey, TValue>
Type Parameters
TKeyCache key type.
TValueCache value type.
- Inheritance
-
TimedCache<TKey, TValue>
- Inherited Members
Constructors
TimedCache(IEqualityComparer<TKey>)
Initializes a new cache with an optional key comparer.
public TimedCache(IEqualityComparer<TKey> comparer = null)
Parameters
comparerIEqualityComparer<TKey>
Methods
Clear()
Clears all cached entries.
public void Clear()
Invalidate(TKey)
Removes a single cached entry.
public void Invalidate(TKey key)
Parameters
keyTKeyCache key to invalidate.
Set(TKey, TValue)
Stores or overwrites a value with the current UTC timestamp.
public void Set(TKey key, TValue value)
Parameters
keyTKeyCache key.
valueTValueValue to cache.
SweepExpired(DateTime, TimeSpan, int, int)
Sweeps entries older than the specified TTL using bounded head-first traversal.
public int SweepExpired(DateTime nowUtc, TimeSpan ttl, int maxScan, int maxRemove)
Parameters
nowUtcDateTimeCurrent UTC timestamp.
ttlTimeSpanEntries older than this duration are eligible for removal.
maxScanintMaximum queue nodes to inspect this sweep.
maxRemoveintMaximum entries to remove this sweep.
Returns
- int
Number of entries removed.
TryGet(TKey, TimeSpan, out TValue)
Tries to retrieve a cached value that was stored within the specified TTL. Does NOT refresh the stored timestamp — entries always expire relative to the time they were written via Set(TKey, TValue).
public bool TryGet(TKey key, TimeSpan ttl, out TValue value)
Parameters
keyTKeyCache key.
ttlTimeSpanMaximum age for the entry to be considered valid.
valueTValueThe cached value if found and still valid.
Returns
- bool
trueif a valid (non-expired) entry was found.