Anarchy.Toolkit.Infrastructure.Caching
1.1.0
See the version list below for details.
dotnet add package Anarchy.Toolkit.Infrastructure.Caching --version 1.1.0
NuGet\Install-Package Anarchy.Toolkit.Infrastructure.Caching -Version 1.1.0
<PackageReference Include="Anarchy.Toolkit.Infrastructure.Caching" Version="1.1.0" />
<PackageVersion Include="Anarchy.Toolkit.Infrastructure.Caching" Version="1.1.0" />
<PackageReference Include="Anarchy.Toolkit.Infrastructure.Caching" />
paket add Anarchy.Toolkit.Infrastructure.Caching --version 1.1.0
#r "nuget: Anarchy.Toolkit.Infrastructure.Caching, 1.1.0"
#:package Anarchy.Toolkit.Infrastructure.Caching@1.1.0
#addin nuget:?package=Anarchy.Toolkit.Infrastructure.Caching&version=1.1.0
#tool nuget:?package=Anarchy.Toolkit.Infrastructure.Caching&version=1.1.0
Anarchy.Toolkit.Infrastructure.Caching
Part of Anarchy I/O Toolkit, a .NET developer platform · Pillar: Infrastructure · Layer: Infrastructure · Package catalog
A typed snapshot cache shared by one writer (a refresher job) and many readers (APIs), built on .NET's HybridCache.
dotnet add package Anarchy.Toolkit.Infrastructure.Caching
CacheSnapshot<T>holds a value and the time it was refreshed.ISnapshotCache<T>reads and writes it.GetAsyncis read-only: a miss returnsnull, stores nothing and runs no factory, so readers fail closed and only the writer fills the cache.SnapshotCacheOptions<T>sets, per cached type, the key, the shared layer's expiration, the local memory layer's time-to-live and the maximum age.SnapshotFreshnessHealthCheck<T>fails while the snapshot is missing or older than its maximum age.
When to use it
Use it when a background job loads data and stores it for other processes to read. The shared layer is any
IDistributedCache: add Anarchy.Toolkit.Infrastructure.Caching.Redis or Anarchy.Toolkit.Infrastructure.Caching.DynamoDb.
Without one, the cache is local to the process.
How to use it
services.AddSnapshotCache(new SnapshotCacheOptions<CarrierList>
{
LocalCacheExpiration = TimeSpan.FromSeconds(30),
MaxAge = TimeSpan.FromMinutes(15)
});
// Writer
await cache.SetAsync(carriers, cancellationToken);
// Reader
var snapshot = await cache.GetAsync(cancellationToken) ?? throw new CarriersUnavailableException();
AddSnapshotCache<T> registers SnapshotFreshnessHealthCheck<T> as its concrete type only, so it is not in the
health reports. AddSnapshotFreshnessHealthCheck<T>() adds it as a scoped IHealthCheck (once per type): it fails
until the first snapshot is stored and again once the snapshot is older than MaxAge. To refresh snapshots on an
interval, AddSnapshotLoader<T, TLoader>() from Anarchy.Toolkit.Runtime.Workers.Caching registers a loader, the
refresher worker and this check.
Registration uses TryAdd, so an application can replace any part. Values are serialized with System.Text.Json
unless you configure another HybridCache serializer. Hits, misses and writes are counted through AppTelemetry
when it is registered.
See the toolkit overview and architecture guide.
Releases
See the release notes for the complete release history.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Anarchy.Toolkit.Core.Health (>= 4.1.0)
- Anarchy.Toolkit.Core.Telemetry (>= 2.2.0)
- Humanizer (>= 3.0.10)
- Microsoft.Extensions.Caching.Hybrid (>= 10.11.1)
- Microsoft.Extensions.Caching.Memory (>= 10.0.15)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.15)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Diagnostics (>= 10.0.15)
- Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.15)
- Microsoft.Extensions.Logging (>= 10.0.15)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.15)
- Newtonsoft.Json (>= 13.0.4)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Anarchy.Toolkit.Infrastructure.Caching:
| Package | Downloads |
|---|---|
|
Anarchy.Toolkit.Runtime.Workers.Caching
Snapshot refresher worker: loads registered snapshots on an interval, in parallel and each with its own timeout, keeping the last good snapshot and reporting freshness through health checks. |
|
|
Anarchy.Toolkit.Infrastructure.Caching.DynamoDb
DynamoDB as the shared layer of Anarchy Toolkit snapshot caches, through AWS's distributed cache provider. |
|
|
Anarchy.Toolkit.Infrastructure.Caching.Redis
Redis as the shared layer of Anarchy Toolkit snapshot caches, through Microsoft's StackExchange Redis distributed cache. |
GitHub repositories
This package is not used by any popular GitHub repositories.
## Releases
### 1.1.0
- Add `AddSnapshotFreshnessHealthCheck<T>()`, which adds the snapshot's freshness check to the health reports (opt-in; `AddSnapshotCache<T>` still registers only the concrete check).
### 1.0.0
- Add `CacheSnapshot<T>`, `ISnapshotCache<T>` over `HybridCache` with a read-only `GetAsync` and per-type options, `SnapshotFreshnessHealthCheck<T>` and `AddSnapshotCache<T>`.