Table of Contents

JsonFileRateCache Class

Definition

Namespace
Bodu.Financial.ExchangeRates.Caching
Assembly
Bodu.Financial.ExchangeRates.Caching.dll
Package
Bodu.Financial.ExchangeRates.Caching 1.0.0
Source
JsonFileRateCache.cs

An IRateCache that persists a provider's rates as JSON files, one file per currency pair (or, under a partitioned layout, per pair and calendar period).

public sealed class JsonFileRateCache : FileRateCacheBase<FileRateCacheOptions>, IFileRateCache, IRateCache
Inheritance
JsonFileRateCache
Implements
Inherited Members
Extension Methods

Examples

A cache bound to provider RBA stores the AUD/USD pair as <directory>/RBA/AUDUSD.json with the self-describing header, one object per dated rate, and one object per fetched window:

{
  "Provider": "RBA",
  "From": "AUD",
  "To": "USD",
  "Entries": [
    { "Date": "2023-01-03", "Rate": 0.5000, "CachedAtUtc": "2023-01-04T09:15:00+00:00", "ObservedAtUtc": "2023-01-03T16:00:00+00:00" }
  ],
  "Coverage": [
    { "Start": "2023-01-03", "End": "2023-01-06", "FetchedAtUtc": "2023-01-04T09:15:00+00:00" }
  ]
}

Remarks

Each file is a JSON object recording the bound Provider and the pair's From and To currency codes - making the file self-describing rather than identified only by its name and folder - alongside an Entries array, one object per dated rate, and a Coverage array, one object per recorded fetch window. Decimal rates are written as JSON numbers, which System.Text.Json round-trips losslessly to decimal; dates and instants use ISO 8601 forms.

A file written before coverage was tracked has no Coverage array; it deserializes to its rate rows with empty coverage and no error, so older caches remain readable. A row written before the upstream fetch instant was tracked has no ObservedAtUtc key and deserializes that field to null, and a file written before the self-describing header was added has no Provider/From/To keys.

Files are laid out by the configured Layout - by default a single file per pair under a per-provider subdirectory, or one file per calendar period when a partitioned layout is selected. Malformed content is treated as an empty result, and all file-level resilience - including atomic temp-and-move writes - is provided by FileRateCacheBase<TOptions>.

Constructors

JsonFileRateCache(FileRateCacheOptions, TimeProvider?, ILogger?)

Initializes a new instance of the JsonFileRateCache class.

public JsonFileRateCache(FileRateCacheOptions options, TimeProvider? timeProvider = null, ILogger? logger = null)

Parameters

options FileRateCacheOptions

The file-cache options that select the bound provider, storage directory, and layout.

timeProvider TimeProvider

The time source the swallowed-failure warning rate-limiting is measured against, or null to use System.

logger ILogger

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

Exceptions

ArgumentNullException

Thrown when options is null.

ArgumentException

Thrown when options fails validation.

Properties

FileExtension

Gets the file extension, including the leading period, applied to cached rate files.

protected override string FileExtension { get; }

Property Value

string

The file extension used by the serialization format, for example .toml.

Applies to

ProductVersions
.NET8, 10