Table of Contents

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, IDisposable

Inheritance

Implements

Inherited Members

Remarks

A request takes one of three paths:

  1. Fresh hit — within the freshness window, served from memory with no network at all.
  2. Stale hit — the entry is past its window but has an ETag, so the request goes out with If-None-Match. The API answers 304 with an empty body, the entry's clock is reset, and the cached body is served. A 22 KB set response costs 0 bytes here.
  3. 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

cache ITcgDexResponseCache

Where responses are stored.

options TcgDexCacheOptions?

Freshness policy. Defaults are used when omitted.

timeProvider TimeProvider?

Clock used for freshness; defaults to the system clock.

Exceptions

ArgumentNullException

cache is null.

Properties

FreshHits

Number of responses served without any network request.

public long FreshHits { get; }

Property Value

long

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

long

Revalidations

Number of entries refreshed by a 304 instead of a re-download.

public long Revalidations { get; }

Property Value

long

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

request HttpRequestMessage

The HTTP request message to send to the server.

cancellationToken CancellationToken

A cancellation token to cancel operation.

Returns

Task<HttpResponseMessage>

The task object representing the asynchronous operation.

Exceptions

ArgumentNullException

The request was null.

OperationCanceledException

The cancellation token was canceled. This exception is stored into the returned task.