FileSystemByteCache<TKey> Class
Definition
- Namespace
- Bodu.Financial.ExchangeRates
- Assembly
- Bodu.Financial.ExchangeRates.dll
- Package
- Bodu.Financial.ExchangeRates 1.0.0
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
TKeyThe type identifying a cached download unit (a date range, feed, era, and so on).
- Inheritance
-
FileSystemByteCache<TKey>
- Implements
-
IByteCache<TKey>
- 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
directorystringThe cache directory, or null/blank to use the default.
defaultFolderNamestringThe folder name (under the system temporary path) to use when
directoryis null or blank.loggerILoggerThe 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
keyTKeyThe 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
keyTKeyThe 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
Store(TKey, byte[])
Stores the bytes for a download unit, replacing any existing entry.
public void Store(TKey key, byte[] bytes)
Parameters
keyTKeyThe download unit the bytes belong to.
bytesbyte[]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
keyTKeyThe download unit the bytes belong to.
bytesbyte[]The response bytes to persist.
Exceptions
- ArgumentNullException
bytesis 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
keyTKeyThe download unit to look up.
refreshIntervalTimeSpanThe maximum age at which a cached response is still considered fresh.
bytesbyte[]When this method returns true, the cached response bytes; otherwise null.
Returns
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
keyTKeyThe download unit to look up.
refreshIntervalTimeSpanThe maximum age a cached file may reach before it is treated as stale.
bytesbyte[]When this method returns true, the cached bytes; otherwise null.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |