SaanSoft.TaggedCache.Memory 0.1.0

dotnet add package SaanSoft.TaggedCache.Memory --version 0.1.0
                    
NuGet\Install-Package SaanSoft.TaggedCache.Memory -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.Memory" 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.Memory" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SaanSoft.TaggedCache.Memory" />
                    
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.Memory --version 0.1.0
                    
#r "nuget: SaanSoft.TaggedCache.Memory, 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.Memory@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.Memory&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SaanSoft.TaggedCache.Memory&version=0.1.0
                    
Install as a Cake Tool

SaanSoft.TaggedCache.Memory

In-memory backend for SaanSoft.TaggedCache — tag-based cache on top of IDistributedCache.

Not recommended for production. This backend stores all cache data in-process. It does not support distributed caching and will produce inconsistencies in multi-instance deployments. Use it for local development and testing only.

Installation

dotnet add package SaanSoft.TaggedCache.Memory

Setup

// Program.cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMemoryTaggedCache();

var app = builder.Build();
app.Run();

Configuration

Pass a MemoryTaggedCacheOptions instance to customise behaviour:

builder.Services.AddMemoryTaggedCache(new MemoryTaggedCacheOptions
{
    // 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:",

    // Customise the underlying IMemoryCache
    ConfigureMemoryCache = memOptions =>
    {
        memOptions.SizeLimit = 1024;
    }
});

Available options

Property Default Description
DefaultCacheOptions 5 min absolute Expiry used when none is supplied to SetAsync
SlidingRefreshThresholdFraction 0.25 Fraction of sliding window remaining at which to proactively refresh; must be > 0 and <= 1
TagKeyPrefix "tag:" Prefix applied to tag storage keys
JsonSerializerOptions JsonSerializerDefaults.Web Controls serialisation of cached values
ConfigureMemoryCache null Optional Action<MemoryCacheOptions> forwarded to AddMemoryCache

DI registration

ITaggedCache is registered as Singleton because the in-process tag index must be shared across all requests. IDistributedCache is also registered, resolving to the same ITaggedCache instance.

Usage

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 79 5/15/2026