Class TcgDexCachingHandler
- Namespace
- TcgDex.Caching
- Assembly
- TcgDex.CSharpSdk.dll
Serves repeated reads from a cache, and revalidates with the API rather than re-downloading when an entry goes stale.
public sealed class TcgDexCachingHandler : DelegatingHandler, IDisposableInheritance
Implements
Inherited Members
Remarks
A request takes one of three paths:
- Fresh hit — within the freshness window, served from memory with no network at all.
-
Stale hit — the entry is past its window but has an
ETag, so the request goes out withIf-None-Match. The API answers304with an empty body, the entry's clock is reset, and the cached body is served. A 22 KB set response costs 0 bytes here. - Miss — a normal request, stored on the way back.
Only GET is cached, which is the whole API — it is read-only.
This sits in the HttpClient pipeline rather than inside the SDK's
transport, so it is transparent to every resource client and composes with
any other handler a caller adds.
Constructors
TcgDexCachingHandler(ITcgDexResponseCache, TcgDexCacheOptions?, TimeProvider?)
Creates the handler.
public TcgDexCachingHandler(ITcgDexResponseCache cache, TcgDexCacheOptions? options = null, TimeProvider? timeProvider = null)Parameters
cacheITcgDexResponseCache-
Where responses are stored.
optionsTcgDexCacheOptions?-
Freshness policy. Defaults are used when omitted.
timeProviderTimeProvider?-
Clock used for freshness; defaults to the system clock.
Exceptions
- ArgumentNullException
-
cacheis null.
Properties
FreshHits
Number of responses served without any network request.
public long FreshHits { get; }Property Value
Remarks
Useful for confirming the cache is doing what you expect.
Misses
Number of requests that went to the API in full.
public long Misses { get; }Property Value
Revalidations
Number of entries refreshed by a 304 instead of a re-download.
public long Revalidations { get; }Property Value
Methods
SendAsync(HttpRequestMessage, CancellationToken)
Sends an HTTP request to the inner handler to send to the server as an asynchronous operation.
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)Parameters
requestHttpRequestMessage-
The HTTP request message to send to the server.
cancellationTokenCancellationToken-
A cancellation token to cancel operation.
Returns
- Task<HttpResponseMessage>
-
The task object representing the asynchronous operation.
Exceptions
- ArgumentNullException
-
The
requestwas null. - OperationCanceledException
-
The cancellation token was canceled. This exception is stored into the returned task.