MonadicSharp.Caching 1.0.0

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

MonadicSharp.Caching

Result-aware caching for AI agent pipelines — cache misses and errors are typed Result<T> values, never null or exceptions.

NuGet .NET


Overview

MonadicSharp.Caching integrates caching into Railway-Oriented pipelines:

  • ICacheService — unified Result-aware interface for memory and distributed caches
  • MemoryCacheService — backed by IMemoryCache, serializes via JSON
  • DistributedCacheService — backed by IDistributedCache (Redis, SQL Server, etc.)
  • CachingAgentWrapper — transparent decorator: caches agent output for identical inputs
  • AgentCachePolicy — configurable key factory, TTL, bypass predicate

Installation

dotnet add package MonadicSharp.Caching

ICacheService

// Get — returns CacheError.Miss on miss, never null
Result<UserProfile> cached = await cache.GetAsync<UserProfile>("user:42");

// Set
await cache.SetAsync("user:42", profile, new CacheEntryOptions
{
    AbsoluteExpiration = TimeSpan.FromMinutes(10)
});

// Remove (idempotent — removing a missing key is always Success)
await cache.RemoveAsync("user:42");

// GetOrSet — the Railway-friendly pattern
var result = await cache.GetOrSetAsync(
    key: $"user:{userId}",
    factory: ct => repository.FindAsync(userId, ct),
    options: CacheEntryOptions.WithTtl(TimeSpan.FromMinutes(5)));

CacheEntryOptions

// Absolute TTL
CacheEntryOptions.WithTtl(TimeSpan.FromMinutes(10))

// Sliding TTL (reset on each access)
new CacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(5) }

// Size (for IMemoryCache with size limits)
new CacheEntryOptions { Size = 1 }

CachingAgentWrapper

Wraps any IAgent<TInput, TOutput> with transparent output caching.

IAgent<Query, SearchResult> cachedSearch = new CachingAgentWrapper<Query, SearchResult>(
    inner: searchAgent,
    cache: cache,
    policy: new AgentCachePolicy<Query, SearchResult>
    {
        // Key derivation from agent name + input
        KeyFactory = (name, q) => $"{name}:{q.Text}:{q.TopK}",

        // Cache TTL
        EntryOptions = CacheEntryOptions.WithTtl(TimeSpan.FromMinutes(10)),

        // Only cache successful results (default: true)
        CacheOnlySuccesses = true,

        // Skip cache for certain inputs
        BypassPredicate = (q, ctx) => q.SkipCache
    });

The wrapper exposes the same Name and RequiredCapabilities as the inner agent — it is completely transparent to the pipeline.


DI Registration

// IMemoryCache-backed (in-process)
services.AddMonadicSharpMemoryCache();

// IDistributedCache-backed (Redis, SQL Server, etc.)
services.AddStackExchangeRedisCache(opts => opts.Configuration = "localhost");
services.AddMonadicSharpDistributedCache();

Error Codes

Code Meaning
CACHE_MISS Key not found in cache
CACHE_DESERIALIZATION_FAILED Cached bytes could not be deserialized
CACHE_SERIALIZATION_FAILED Value could not be serialized before storing
CACHE_STORE_ERROR Underlying cache store threw an exception

License

MIT — part of MonadicSharp.Framework.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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 MonadicSharp.Caching:

Package Downloads
MonadicSharp.Framework

Meta-package for the MonadicSharp Framework — install this single package to get Agents, Caching, Http, Persistence, Security, and Telemetry in one shot. For à-la-carte usage, reference individual MonadicSharp.* packages instead.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 39 3/3/2026