Table of Contents

FileSystemImfReportCache Class

Definition

Namespace
Bodu.Financial.ExchangeRates
Assembly
Bodu.Financial.ExchangeRates.Imf.dll
Package
Bodu.Financial.ExchangeRates.Imf 0.7.2
Source
FileSystemImfReportCache.cs

An IImfReportCache that persists downloaded IMF monthly reports as files in a cache directory.

public sealed class FileSystemImfReportCache : FileSystemByteCache<ImfReportMonth>, IByteCache<ImfReportMonth>, IImfReportCache
Inheritance
FileSystemImfReportCache
Implements
Inherited Members
Extension Methods

Remarks

Each report is stored under its month's file name in the cache directory; the file's last-write time serves as the freshness timestamp. A report written after its month had closed (in a later UTC month) holds every day of the month and never changes, so it is always served regardless of its age. A report written while its month was still open is treated as stale once it exceeds the supplied refresh interval, forcing a re-download, even after the month has closed, because it lacks the days the IMF published after it was written.

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

FileSystemImfReportCache(string?, ILogger?)

Initializes a new instance of the FileSystemImfReportCache class.

public FileSystemImfReportCache(string? directory, ILogger? logger = null)

Parameters

directory string

The cache directory. When null or empty, a bodu-imf folder under the system temporary path is used.

logger ILogger

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

Methods

GetFileName(ImfReportMonth)

Derives the cache file name for a key.

protected override string GetFileName(ImfReportMonth key)

Parameters

key ImfReportMonth

The download unit to name.

Returns

string

The cache file name.

Exceptions

ArgumentNullException

key is null.

IsFresh(ImfReportMonth, TimeSpan, TimeSpan)

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

protected override bool IsFresh(ImfReportMonth key, TimeSpan age, TimeSpan refreshInterval)

Parameters

key ImfReportMonth

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.

Remarks

A report written in a UTC month later than its own was written after the month closed, so it is always fresh regardless of its file age; a report written during its own month (or earlier) is fresh only while its age does not exceed refreshInterval.

Exceptions

ArgumentNullException

key is null.

Store(ImfReportMonth, byte[])

Stores the bytes for a report month, replacing any existing entry.

public void Store(ImfReportMonth month, byte[] bytes)

Parameters

month ImfReportMonth

The report month whose file is being cached.

bytes byte[]

The report bytes to store.

Remarks

Intentionally hides the unguarded base member so calls through this type (and through IImfReportCache) validate their arguments before touching the file system.

TryGet(ImfReportMonth, TimeSpan, out byte[])

Attempts to retrieve the cached bytes for a report month.

public bool TryGet(ImfReportMonth month, TimeSpan refreshInterval, out byte[] bytes)

Parameters

month ImfReportMonth

The report month whose file is requested.

refreshInterval TimeSpan

The maximum age at which a current-month report is still considered fresh.

bytes byte[]

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

Returns

bool

true when a fresh cache entry exists; otherwise false.

Remarks

Intentionally hides the unguarded base member so calls through this type (and through IImfReportCache) validate their arguments before touching the file system.

TryGet(ImfReportMonth, TimeSpan, out byte[], out DateTimeOffset?)

Attempts to retrieve the cached bytes for a report month, together with the instant they were stored.

public bool TryGet(ImfReportMonth month, TimeSpan refreshInterval, out byte[] bytes, out DateTimeOffset? storedAtUtc)

Parameters

month ImfReportMonth

The report month whose file is requested.

refreshInterval TimeSpan

The maximum age at which a current-month report is still considered fresh.

bytes byte[]

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

storedAtUtc DateTimeOffset?

When this method returns true, the instant the entry was stored, or null when the cache does not record it; otherwise null.

Returns

bool

true when a fresh cache entry exists; otherwise false.

Remarks

The provider stamps a month served from the cache with storedAtUtc, the time its bytes were fetched from the IMF, rather than the time they were read back. The default implementation calls TryGet(ImfReportMonth, TimeSpan, out byte[]) and reports no stored time.

Exceptions

ArgumentNullException

month is null.

Applies to

ProductVersions
.NET8, 10