ForgeTrust.AppSurface.Caching 0.2.0-preview.15

This is a prerelease version of ForgeTrust.AppSurface.Caching.
dotnet add package ForgeTrust.AppSurface.Caching --version 0.2.0-preview.15
                    
NuGet\Install-Package ForgeTrust.AppSurface.Caching -Version 0.2.0-preview.15
                    
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="ForgeTrust.AppSurface.Caching" Version="0.2.0-preview.15" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ForgeTrust.AppSurface.Caching" Version="0.2.0-preview.15" />
                    
Directory.Packages.props
<PackageReference Include="ForgeTrust.AppSurface.Caching" />
                    
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 ForgeTrust.AppSurface.Caching --version 0.2.0-preview.15
                    
#r "nuget: ForgeTrust.AppSurface.Caching, 0.2.0-preview.15"
                    
#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 ForgeTrust.AppSurface.Caching@0.2.0-preview.15
                    
#: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=ForgeTrust.AppSurface.Caching&version=0.2.0-preview.15&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ForgeTrust.AppSurface.Caching&version=0.2.0-preview.15&prerelease
                    
Install as a Cake Tool

ForgeTrust.AppSurface.Caching

Caching primitives for AppSurface applications built on top of Microsoft.Extensions.Caching.Memory.

Overview

This package provides a small, focused caching layer for AppSurface modules. It is designed for scenarios where you want consistent memoization behavior, cache policies, and a module you can register into the AppSurface startup pipeline.

Release Guidance

AppSurface ships as a coordinated package family. Before installing this package from a prerelease feed, check the package chooser and release hub for current release risk, migration guidance, and readiness.

Key Types

  • AppSurfaceCachingModule: Registers the package services into the AppSurface module system.
  • IMemo / Memo: Memoization helpers for caching computed values and async results.
  • CachePolicy: A simple policy object for configuring expiration and cache behavior.

Usage

In the class that implements IAppSurfaceModule, register the caching dependency in RegisterDependentModules. AppSurfaceCachingModule calls AddMemoryCache() and registers IMemo for injection into application services.

public void RegisterDependentModules(ModuleDependencyBuilder builder)
{
    builder.AddModule<AppSurfaceCachingModule>();
}

Use IMemoryCache directly inside caching infrastructure or at a framework integration point that cannot take IMemo. Keep one owner for each cached snapshot. In AppSurface Docs, DocAggregator owns the document and search-index caches so controllers can read the same snapshot.

For an expensive lookup, inject IMemo and choose how long to keep the result.

var result = await memo.GetAsync(
    () => LoadDocsAsync(),
    CachePolicy.Absolute(TimeSpan.FromMinutes(5)));

Use stale-while-revalidate for expensive values that should stay responsive after their freshness window expires:

var result = await memo.GetAsync(
    () => LoadDocsAsync(),
    CachePolicy.AbsoluteWithStaleWhileRevalidate(
        freshDuration: TimeSpan.FromMinutes(5),
        staleDuration: TimeSpan.FromMinutes(5)));

Notes

Deterministic freshness transitions

The existing Memo(IMemoryCache) and Memo(IMemoryCache, TimeSpan failureCacheDuration) constructors use TimeProvider.System. The explicit Memo(IMemoryCache, TimeSpan failureCacheDuration, TimeProvider) overload lets a host or test control the timestamps used to store and compare fresh/stale windows. The cache and clock must be nonnull, and the failure duration must be positive. It borrows both dependencies and retains the existing cache policies and cancellation semantics.

Use the explicit overload when verifying phase boundaries or coordinating an application's existing clock. Configure the supplied MemoryCache with an ISystemClock adapter backed by that same provider through MemoryCacheOptions.Clock. Memo does not replace the cache's expiration clock. Two independent clocks can evict an entry while Memo still considers it stale, or classify an entry as expired while its cache still retains it. Move a custom clock forward only; ordinary constructors require no adapter or configuration change.

Clock advancement changes logical freshness, not task completion. Hold/release the actual factory independently, join caller tasks, and verify both a stale caller's immediate old value and an expired caller's awaited failure. Increasing a delay or TTL does not establish which phase a scheduled continuation observed. The Docs snapshot regression control uses the real Memo and MemoryCache with a shared clock to check these cases without a wall-clock race.

Memo.DrainBackgroundRefreshesAsync(CancellationToken) joins a snapshot of original refresh tasks already registered by completed stale reads. Completion means those tasks ended, including failed or cancelled refreshes; it does not mean a refresh succeeded or supply a cached value. Stale-refresh errors remain observed, while expired caller refreshes continue to propagate their own failures. Caller cancellation stops only the drain wait; accepted factories remain owned by their original tasks. No timeout or fresh TTL is created.

For deterministic observation or graceful shutdown, stop new requests, await already accepted read calls, drain their background snapshot under your existing cancellation/deadline, then dispose Memo and its borrowed cache. Reads accepted later are outside that snapshot, and synchronous Dispose does not promise an asynchronous join. The Memo ownership controls hold a real refresh to verify pending drainage, cancellation without factory cancellation, and publication of the refreshed value after its original join.

Clock configuration and drainage belong to the concrete Memo owner; IMemo remains the read API. The default module registration uses the system clock. Registering a TimeProvider alone does not configure Memo or the cache. If a host needs a custom clock or explicit shutdown drainage, register an instance before the module configures its defaults, retain that same instance in the host, and expose it to readers as IMemo:

// sharedClock is the host's TimeProvider; sharedCache uses an ISystemClock adapter over it.
var ownedMemo = new Memo(sharedCache, TimeSpan.FromSeconds(1), sharedClock);
services.AddSingleton<IMemo>(ownedMemo);

// After the host has stopped and joined accepted reads:
await ownedMemo.DrainBackgroundRefreshesAsync(shutdownToken);
ownedMemo.Dispose();
// The host also disposes sharedCache when its other users have stopped.

The host owns disposal of an instance supplied to dependency injection this way. Consumers with a custom IMemo implementation retain that implementation's lifecycle contract.

General notes

  • The package builds on Microsoft.Extensions.Caching.Memory, so it works well for in-process application caching.
  • CachePolicy.AbsoluteWithStaleWhileRevalidate returns the stale value immediately during the stale window and starts one background refresh for that cache key. If the background refresh fails, the stale value remains available until the stale window ends.
  • Stale-while-revalidate is supported for absolute-expiration policies. Sliding expiration does not have a stable revalidation moment, so use CachePolicy.AbsoluteWithStaleWhileRevalidate(...) or CachePolicy.Absolute(...).WithStaleWhileRevalidate(...).
  • Memo constructors without an explicit clock use system UTC. The public Memo(IMemoryCache, TimeSpan failureCacheDuration, TimeProvider) overload supplies timestamps for fresh/stale comparisons; configure the backing MemoryCache with the same clock. The internal Func<DateTimeOffset> overload remains a test seam. Advance both clocks together to test fresh, stale and failure-expiration boundaries; changing only the storage clock leaves stale-value classification on a different timeline. A custom clock does not change any duration or cache policy.
  • This package is intentionally lightweight and fits best when you want simple, application-level caching rather than a distributed cache abstraction.
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 (1)

Showing the top 1 NuGet packages that depend on ForgeTrust.AppSurface.Caching:

Package Downloads
ForgeTrust.AppSurface.Docs

ForgeTrust.AppSurface.Docs package for AppSurface application composition.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0-preview.15 485 11/24/2026
0.2.0-preview.14 1,419 10/16/2026
0.2.0-preview.13 373 10/4/2026
0.2.0-preview.11 87 9/30/2026
0.2.0-preview.8 2,435 8/23/2026
0.2.0-preview.7 301 8/16/2026
0.2.0-preview.6 216 8/12/2026
0.2.0-preview.5 249 8/1/2026
0.2.0-preview.4 194 7/18/2026
0.2.0-preview.2 93 7/3/2026
0.2.0-preview.1 96 6/28/2026
0.1.0 717 7/2/2026
0.1.0-rc.4 111 6/16/2026
0.1.0-rc.3 117 6/8/2026
0.1.0-rc.2 97 6/3/2026
0.1.0-rc.1 106 5/31/2026
0.1.0-preview.4 93 5/25/2026
0.1.0-preview.3 98 5/20/2026
0.1.0-preview.2 101 5/14/2026