Table of Contents

Class MemoryTcgDexResponseCache

Namespace
TcgDex.Caching
Assembly
TcgDex.CSharpSdk.dll

The default in-process response cache: bounded, thread-safe, and evicting the least recently used entries when full.

public sealed class MemoryTcgDexResponseCache : ITcgDexResponseCache

Inheritance

Implements

Inherited Members

Remarks

Deliberately not built on IMemoryCache. That would add a package reference for a dictionary with an eviction policy, and it prices entries by an arbitrary "size" unit; here the natural bound is a count of responses, which is directly meaningful.

Eviction tracks last access rather than insertion, so the entries an application actually reads survive — which for this API means the enumeration endpoints it hits on every screen. The bounding and eviction policy itself lives in TcgDex.Caching.BoundedLru`2, shared with the deserialized-response cache; what this type adds is the absolute lifetime.

Constructors

MemoryTcgDexResponseCache(int, TimeProvider?)

Creates a cache holding at most maxEntries responses.

public MemoryTcgDexResponseCache(int maxEntries = 512, TimeProvider? timeProvider = null)

Parameters

maxEntries int

The upper bound on stored entries.

timeProvider TimeProvider?

Clock used for expiry; defaults to the system clock.

Exceptions

ArgumentOutOfRangeException

maxEntries is less than one.

Properties

Count

The number of entries currently held.

public int Count { get; }

Property Value

int

Methods

Clear()

Empties the cache.

public void Clear()

GetAsync(string, CancellationToken)

Retrieves an entry, or null if absent.

public ValueTask<CachedResponse?> GetAsync(string key, CancellationToken cancellationToken = default)

Parameters

key string

The cache key, derived from the request URI.

cancellationToken CancellationToken

Cancels the lookup.

Returns

ValueTask<CachedResponse?>

The cached response, or null.

RemoveAsync(string, CancellationToken)

Removes an entry, if present.

public ValueTask RemoveAsync(string key, CancellationToken cancellationToken = default)

Parameters

key string

The cache key.

cancellationToken CancellationToken

Cancels the removal.

Returns

ValueTask

A task that completes when the entry is gone.

SetAsync(string, CachedResponse, TimeSpan, CancellationToken)

Stores an entry.

public ValueTask SetAsync(string key, CachedResponse response, TimeSpan timeToLive, CancellationToken cancellationToken = default)

Parameters

key string

The cache key.

response CachedResponse

The response to store.

timeToLive TimeSpan

How long it may be served before revalidation.

cancellationToken CancellationToken

Cancels the write.

Returns

ValueTask

A task that completes when the entry is stored.