Table of Contents

FileSystemImfReportCache Class

Definition

Namespace
Bodu.Financial.ExchangeRates
Assembly
Bodu.Financial.ExchangeRates.Imf.dll
Package
Bodu.Financial.ExchangeRates.Imf 0.7.1
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 for a month strictly before the current UTC month is immutable and is therefore always served regardless of its age; the report for the current month is treated as stale once it exceeds the supplied refresh interval, forcing a re-download.

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 for a month strictly before the current UTC month is closed and immutable, so it is always fresh regardless of its file age; only the current (still-accumulating) month is subject to the refresh interval.

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.

Applies to

ProductVersions
.NET8, 10