Table of Contents

DistributedRateCache Class

Definition

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

An IRateCache that persists a single provider's rates and fetch-coverage windows in an injected IDistributedCache - for example a Redis cache - expiring them through the same freshness mechanism as the in-memory, TOML, and SQLite caches.

public sealed class DistributedRateCache : RateCacheBase<DistributedRateCacheOptions>, IRateCache
Inheritance
DistributedRateCache
Implements
Inherited Members
Extension Methods

Examples

// A distributed caching provider backed by a Redis IDistributedCache resolved from DI.
var options = new DistributedRateCacheOptions { Provider = "RBA" };
var cache = new DistributedRateCache(redisDistributedCache, options);
IDatedRateProvider cached = new CachingRateProvider(rba, cache, new CachingRateOptions());

Remarks

The full per-pair state is stored as a single JSON blob under one stable, collision-free key, {prefix}{provider}:{from}{to}. The blob carries both the cached rate rows and the recorded coverage windows. Decimal rates are serialized as invariant strings and all dates and instants as invariant ISO text (a DateOnly as yyyy-MM-dd, a DateTimeOffset in round-trip "O" form) so the full precision and scale round-trips losslessly, mirroring the TOML and SQLite caches' string-decimal choice.

Because a pair's whole state travels as one blob, this backend fits the whole-state seam of RateCacheBase<TOptions> exactly: the read-merge-write mechanism, per-pair locking, snapshot read, and the shared RateCacheRules policy are all inherited, and this class contributes only the blob serialization. A single StoreFetchedRange(CurrencyPair, IReadOnlyList<CachedRate>, DateOnly, DateOnly, TimeSpan, DateTimeOffset) set is all-or-nothing - the reader never observes coverage without its rows even across processes - while cross-process concurrent writes to the same pair remain last-write-wins, consistent with the documented best-effort nature of the contract.

As required by IRateCache, a backing-store failure surfaces as an empty read or a skipped write rather than an exception: IDistributedCache faults and JSON (de)serialization faults degrade gracefully, while argument validation still throws.

Constructors

DistributedRateCache(IDistributedCache, DistributedRateCacheOptions, TimeProvider?, ILogger?)

Initializes a new instance of the DistributedRateCache class.

public DistributedRateCache(IDistributedCache cache, DistributedRateCacheOptions options, TimeProvider? timeProvider = null, ILogger? logger = null)

Parameters

cache IDistributedCache

The distributed cache the per-pair blobs are persisted in.

options DistributedRateCacheOptions

The options carrying the bound provider and the optional key prefix.

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 cache or options is null.

ArgumentException

Thrown when options fails validation.

DistributedRateCache(IDistributedCache, string)

Initializes a new instance of the DistributedRateCache class bound to a provider over the supplied distributed cache.

public DistributedRateCache(IDistributedCache cache, string provider)

Parameters

cache IDistributedCache

The distributed cache the per-pair blobs are persisted in.

provider string

The provider the cache stores rates for.

Exceptions

ArgumentNullException

Thrown when cache is null, or when provider is null.

ArgumentException

Thrown when provider is empty or white space.

Applies to

ProductVersions
.NET8, 10