FileSystemImfReportCache Class
Definition
- Namespace
- Bodu.Financial.ExchangeRates
- Assembly
- Bodu.Financial.ExchangeRates.Imf.dll
- Package
- Bodu.Financial.ExchangeRates.Imf 0.7.2
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
Methods
GetFileName(ImfReportMonth)
Derives the cache file name for a key.
protected override string GetFileName(ImfReportMonth key)
Parameters
keyImfReportMonthThe download unit to name.
Returns
- string
The cache file name.
Exceptions
- ArgumentNullException
keyis 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
keyImfReportMonthThe download unit the file was cached for.
ageTimeSpanThe elapsed time since the file was last written.
refreshIntervalTimeSpanThe 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
keyis null.
Store(ImfReportMonth, byte[])
Stores the bytes for a report month, replacing any existing entry.
public void Store(ImfReportMonth month, byte[] bytes)
Parameters
monthImfReportMonthThe report month whose file is being cached.
bytesbyte[]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
monthImfReportMonthThe report month whose file is requested.
refreshIntervalTimeSpanThe maximum age at which a current-month report is still considered fresh.
bytesbyte[]When this method returns true, the cached report bytes; otherwise null.
Returns
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
monthImfReportMonthThe report month whose file is requested.
refreshIntervalTimeSpanThe maximum age at which a current-month report is still considered fresh.
bytesbyte[]When this method returns true, the cached report bytes; otherwise null.
storedAtUtcDateTimeOffset?When this method returns true, the instant the entry was stored, or null when the cache does not record it; otherwise null.
Returns
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
monthis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |