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
cacheIDistributedCacheThe distributed cache the per-pair blobs are persisted in.
optionsDistributedRateCacheOptionsThe options carrying the bound provider and the optional key prefix.
timeProviderTimeProviderThe time source the swallowed-failure warning rate-limiting is measured against, or null to use System.
loggerILoggerThe 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
cacheoroptionsis null.- ArgumentException
Thrown when
optionsfails 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
cacheIDistributedCacheThe distributed cache the per-pair blobs are persisted in.
providerstringThe provider the cache stores rates for.
Exceptions
- ArgumentNullException
- ArgumentException
Thrown when
provideris empty or white space.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |