Table of Contents

Class CachingAssetSource

Namespace
EncDotNet.S100.Core
Assembly
EncDotNet.S100.Core.dll

Wraps another IAssetSource and memoises the bytes of each asset on first read. Intended for read-only sources whose contents are immutable for the lifetime of the source (EmbeddedAssetSource, packaged exchange sets opened in Read, on-disk spec content).

public sealed class CachingAssetSource : IAssetSource, IDisposable, IAsyncDisposable
Inheritance
CachingAssetSource
Implements
Inherited Members

Remarks

The cache is thread-safe; concurrent first-read storms collapse to a single underlying open via Lazy<T> with ExecutionAndPublication.

Cache size is bounded by the number of distinct relative paths requested and is not actively evicted. For bundled S-100 specification content (Feature and Portrayal Catalogues — see S-100 Edition 5.2.1 Part 4 §4 and Part 9 §3) the working set is small (tens of paths per spec) and steady-state reads dominate.

On cache hits, the supplied CancellationToken is observed only insofar as the call is synchronous: the cached bytes are returned immediately. On a cache miss the token flows through to the underlying OpenAsync(string, CancellationToken).

Constructors

CachingAssetSource(IAssetSource)

Initialises a new CachingAssetSource over the given inner source.

public CachingAssetSource(IAssetSource inner)

Parameters

inner IAssetSource

The asset source to wrap. Must not be null.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

OpenAsync(string, CancellationToken)

Opens a readable stream for the asset at the given relative path within the source.

public Task<Stream> OpenAsync(string relativePath, CancellationToken cancellationToken = default)

Parameters

relativePath string

A forward-slash separated relative path to the asset.

cancellationToken CancellationToken

A cancellation token.

Returns

Task<Stream>

ReadAllBytesAsync(string, CancellationToken)

Returns the cached AssetBytes for relativePath, reading from the underlying source on first access. Overrides the ReadAllBytesAsync(string, CancellationToken) default so that callers referencing this instance through IAssetSource get the cached bytes without a stream round-trip.

public Task<AssetBytes> ReadAllBytesAsync(string relativePath, CancellationToken cancellationToken = default)

Parameters

relativePath string

A forward-slash relative path.

cancellationToken CancellationToken

A cancellation token. Observed on cache misses (when the inner OpenAsync(string, CancellationToken) is invoked); ignored on cache hits.

Returns

Task<AssetBytes>