Table of Contents

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

TKey

Cache key type.

TValue

Cache 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

comparer IEqualityComparer<TKey>

Methods

Clear()

Clears all cached entries.

public void Clear()

Invalidate(TKey)

Removes a single cached entry.

public void Invalidate(TKey key)

Parameters

key TKey

Cache key to invalidate.

Set(TKey, TValue)

Stores or overwrites a value with the current UTC timestamp.

public void Set(TKey key, TValue value)

Parameters

key TKey

Cache key.

value TValue

Value 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

nowUtc DateTime

Current UTC timestamp.

ttl TimeSpan

Entries older than this duration are eligible for removal.

maxScan int

Maximum queue nodes to inspect this sweep.

maxRemove int

Maximum 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

key TKey

Cache key.

ttl TimeSpan

Maximum age for the entry to be considered valid.

value TValue

The cached value if found and still valid.

Returns

bool

true if a valid (non-expired) entry was found.