Table of Contents

FileSystemByteCache<TKey> Class

Definition

Namespace
Bodu.Financial.ExchangeRates
Assembly
Bodu.Financial.ExchangeRates.dll
Package
Bodu.Financial.ExchangeRates 1.0.0
Source
FileSystemByteCache{T}.cs

Provides a best-effort, file-backed cache of downloaded response bytes keyed by a provider-specific download unit.

public abstract class FileSystemByteCache<TKey> : IByteCache<TKey>

Type Parameters

TKey

The type identifying a cached download unit (a date range, feed, era, and so on).

Inheritance
FileSystemByteCache<TKey>
Implements
Derived
Inherited Members
Extension Methods

Remarks

Each key maps to a single file in the cache directory, named by GetFileName(TKey); the file's last-write time serves as its freshness timestamp. A cached file is served only while IsFresh(TKey, TimeSpan, TimeSpan) accepts its age against the supplied refresh interval.

The cache is best-effort: any I/O failure while reading is reported as a miss, and any failure while writing is swallowed, so a cache problem never breaks rate retrieval.

Constructors

FileSystemByteCache(string?, string, ILogger?)

Initializes a new instance of the FileSystemByteCache<TKey> class.

protected FileSystemByteCache(string? directory, string defaultFolderName, ILogger? logger = null)

Parameters

directory string

The cache directory, or null/blank to use the default.

defaultFolderName string

The folder name (under the system temporary path) to use when directory is null or blank.

logger ILogger

The logger that receives a warning when a best-effort file-system failure is swallowed, or null to disable that reporting.

Properties

Directory

Gets the directory in which cached response bytes are stored.

public string Directory { get; }

Property Value

string

The absolute or relative cache directory path.

Methods

GetFileName(TKey)

Derives the cache file name for a key.

protected abstract string GetFileName(TKey key)

Parameters

key TKey

The download unit to name.

Returns

string

The cache file name.

IsFresh(TKey, TimeSpan, TimeSpan)

Determines whether a cached file of the supplied age is still fresh for a key.

protected virtual bool IsFresh(TKey key, TimeSpan age, TimeSpan refreshInterval)

Parameters

key TKey

The download unit the file was cached for.

age TimeSpan

The elapsed time since the file was last written.

refreshInterval TimeSpan

The maximum age a cached file may reach before it is treated as stale.

Returns

bool

true when the file may still be served; otherwise false. The default treats a file as fresh while its age does not exceed refreshInterval.

Store(TKey, byte[])

Stores the bytes for a download unit, replacing any existing entry.

public void Store(TKey key, byte[] bytes)

Parameters

key TKey

The download unit the bytes belong to.

bytes byte[]

The response bytes to store.

StoreCore(TKey, byte[])

Writes the response bytes for a key, swallowing any I/O failure.

protected void StoreCore(TKey key, byte[] bytes)

Parameters

key TKey

The download unit the bytes belong to.

bytes byte[]

The response bytes to persist.

Exceptions

ArgumentNullException

bytes is null.

TryGet(TKey, TimeSpan, out byte[])

Attempts to retrieve the cached bytes for a download unit.

public bool TryGet(TKey key, TimeSpan refreshInterval, out byte[] bytes)

Parameters

key TKey

The download unit to look up.

refreshInterval TimeSpan

The maximum age at which a cached response is still considered fresh.

bytes byte[]

When this method returns true, the cached response bytes; otherwise null.

Returns

bool

true when a fresh cache entry exists; otherwise false.

TryGetCore(TKey, TimeSpan, out byte[])

Attempts to read the cached bytes for a key when a fresh file exists.

protected bool TryGetCore(TKey key, TimeSpan refreshInterval, out byte[] bytes)

Parameters

key TKey

The download unit to look up.

refreshInterval TimeSpan

The maximum age a cached file may reach before it is treated as stale.

bytes byte[]

When this method returns true, the cached bytes; otherwise null.

Returns

bool

true when fresh bytes were read; otherwise false.

Applies to

ProductVersions
.NET8, 10