SaanSoft.TaggedCache.AwsDynamoDb
0.1.0
dotnet add package SaanSoft.TaggedCache.AwsDynamoDb --version 0.1.0
NuGet\Install-Package SaanSoft.TaggedCache.AwsDynamoDb -Version 0.1.0
<PackageReference Include="SaanSoft.TaggedCache.AwsDynamoDb" Version="0.1.0" />
<PackageVersion Include="SaanSoft.TaggedCache.AwsDynamoDb" Version="0.1.0" />
<PackageReference Include="SaanSoft.TaggedCache.AwsDynamoDb" />
paket add SaanSoft.TaggedCache.AwsDynamoDb --version 0.1.0
#r "nuget: SaanSoft.TaggedCache.AwsDynamoDb, 0.1.0"
#:package SaanSoft.TaggedCache.AwsDynamoDb@0.1.0
#addin nuget:?package=SaanSoft.TaggedCache.AwsDynamoDb&version=0.1.0
#tool nuget:?package=SaanSoft.TaggedCache.AwsDynamoDb&version=0.1.0
SaanSoft.TaggedCache.AwsDynamoDb
AWS DynamoDB backend for SaanSoft.TaggedCache — tag-based cache on top of IDistributedCache, backed by two DynamoDB tables.
Installation
dotnet add package SaanSoft.TaggedCache.AwsDynamoDb
DynamoDB tables
This backend requires two DynamoDB tables (names are configurable via DynamoDbTaggedCacheOptions):
| Table | Default name | Purpose |
|---|---|---|
| Cache | Cache |
Stores the serialised cache entries |
| Tags | CacheTags |
Stores the tag-to-key index used for xxByTagAsync operations |
Creating tables
SaanSoft.TaggedCache.AwsDynamoDb provides a ConfigureDynamoDbTaggedCacheTables extension method on IServiceProvider that creates both tables if they do not already exist.
Call it once during application startup before the app starts handling requests.
The ConfigureDynamoDbTaggedCacheTables requires a CacheTableOptions object, which has DynamoDb setup for things like:
- BillingMode
- OnDemandThroughput
- ... (and other standard DynamoDb table creation options)
NOTE: The following fields in CacheTableOptions will be overridden with SaanSoft.TaggedCache.AwsDynamoDb specific configuration, so don't provide these:
- TableName (populated from
DynamoDbTaggedCacheOptions) - KeySchema
- AttributeDefinitions
KeySchema and AttributeDefinitions are created as per below for each table.
Check the Setup section below for an example of running during app startup.
Table schemas
Cache table (CacheTableName):
| Attribute | Type | Key | Notes |
|---|---|---|---|
CacheKey |
String | Hash (partition) | |
ExpiresAtUnix |
Number | — | Unix epoch seconds; used as the DynamoDB TTL attribute |
Tags table (TagTableName):
| Attribute | Type | Key | Notes |
|---|---|---|---|
Tag |
String | Hash (partition) | |
CacheKey |
String | Range (sort) | |
ExpiresAtUnix |
Number | — | Unix epoch seconds; used as the DynamoDB TTL attribute |
DynamoDB TTL
Both tables use ExpiresAtUnix as the DynamoDB Time to Live (TTL) attribute.
TTL is configured automatically by ConfigureDynamoDbTaggedCacheTables — no manual setup is required.
If you manage tables externally (e.g. via Terraform or CloudFormation), ensure the schemas match the above and enable TTL on ExpiresAtUnix for both tables.
Setup
You must register IAmazonDynamoDB yourself before calling AddDynamoDbTaggedCache.
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// Register the DynamoDB client (required)
// Uses credentials and region from the standard AWS SDK configuration chain (environment variables, `appsettings.json`, EC2/ECS instance metadata, etc)
builder.Services.AddAWSService<IAmazonDynamoDB>();
// Register the tagged cache
builder.Services.AddDynamoDbTaggedCache();
var app = builder.Build();
// Create DynamoDb tables (and their indexes) if they don't exist during startup
// The method is safe to call on every startup
// To update existing tables with the options (i.e. "Upsert Table"), provide the parameter `updateExisting=true` to the function
await app.Services.ConfigureDynamoDbTaggedCacheTables(new CacheTableOptions
{
BillingMode = BillingMode.PAY_PER_REQUEST
});
app.Run();
Configuration
Pass a DynamoDbTaggedCacheOptions instance to customise behaviour (if not provided, it defaults to using "new DynamoDbTaggedCacheOptions()" and its defaults):
builder.Services.AddDynamoDbTaggedCache(new DynamoDbTaggedCacheOptions
{
// DynamoDB table names (if different from the defaults)
CacheTableName = "MyApp_Cache",
TagTableName = "MyApp_CacheTags",
// Max retries on DynamoDB read/write operations
MaxRetries = 3,
// Default expiry when none is specified in SetAsync calls
DefaultCacheOptions = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
},
// Refresh a sliding-expiry entry when this fraction of its window remains
SlidingRefreshThresholdFraction = 0.25,
// Prefix added to tag storage keys (set to "" to disable)
TagKeyPrefix = "tag:"
});
DI registration
DynamoDbTaggedCacheOptionsis registered as SingletonITaggedCacheis registered as Scoped; all cache state lives in DynamoDBIDistributedCacheis also registered Scoped, resolving to theITaggedCacheinstance
Usage
app.MapGet("/products", async (ITaggedCache cache) =>
{
var products = await cache.GetOrCreateAsync<List<Product>>(
cacheKey: "products",
factory: ct => await db.GetProductsAsync(ct),
tags: ["products"]
);
return product;
});
app.MapGet("/products/{id}", async (int id, ITaggedCache cache) =>
{
var product = await cache.GetAsync<Product>($"product:{id}");
if (product is null)
{
product = await db.GetProductAsync(id);
await cache.SetAsync($"product:{id}", product, tags: ["products"]);
}
return product;
});
app.MapPost("/products/invalidate", async (ITaggedCache cache) =>
{
await cache.RemoveByTagAsync("products");
});
| 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
- AWSSDK.DynamoDBv2 (>= 4.0.10.5)
- SaanSoft.TaggedCache (>= 0.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 42 | 5/15/2026 |