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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SaanSoft.TaggedCache.AwsDynamoDb" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SaanSoft.TaggedCache.AwsDynamoDb" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SaanSoft.TaggedCache.AwsDynamoDb" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SaanSoft.TaggedCache.AwsDynamoDb --version 0.1.0
                    
#r "nuget: SaanSoft.TaggedCache.AwsDynamoDb, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SaanSoft.TaggedCache.AwsDynamoDb@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SaanSoft.TaggedCache.AwsDynamoDb&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SaanSoft.TaggedCache.AwsDynamoDb&version=0.1.0
                    
Install as a Cake Tool

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

  • DynamoDbTaggedCacheOptions is registered as Singleton
  • ITaggedCache is registered as Scoped; all cache state lives in DynamoDB
  • IDistributedCache is also registered Scoped, resolving to the ITaggedCache instance

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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