Memoization.Net 1.3.0

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

Memoization.Net

A memoization library for .NET that caches function results in IMemoryCache to avoid redundant computations.

NuGet License: LGPL-3.0-or-later WITH LGPL-3.0-linking-exception

Features

  • Built on Microsoft.Extensions.Caching.Abstractions (IMemoryCache)
  • Supports functions with 1 to 16 parameters via source-generated Memoization.Create overloads
  • Supports a shared Memoization.DefaultCache or per-call IMemoryCache instances
  • Supports MemoryCacheEntryOptions (expiration, priority, size limits)
  • Failed invocations throw without populating the cache
  • Multi-targeting: netstandard2.0, netstandard2.1, net462, net10.0, and net11.0

Installation

dotnet add package Memoization.Net
# Required for MemoryCache / MemoryCacheOptions used in the examples below
dotnet add package Microsoft.Extensions.Caching.Memory

Quick Start

Pass an explicit IMemoryCache instance (for example, a MemoryCache you manage or an IMemoryCache injected via dependency injection) to Memoization.Create:

using Microsoft.Extensions.Caching.Memory;
using MemoizationFactory = Memoization.Memoization;

using var cache = new MemoryCache(new MemoryCacheOptions());

static int Fibonacci(int n) =>
    n < 2 ? 1 : Fibonacci(n - 1) + Fibonacci(n - 2);

var memoizedFib = MemoizationFactory.Create<int, int>(Fibonacci, cache);

int first = memoizedFib(10);  // Computes and caches
int second = memoizedFib(10); // Returns cached result

In dependency-injected applications, inject IMemoryCache from the DI container and pass it to Memoization.Create(func, cache).

Overloads & Advanced Usage

Cache Entry Options

using Microsoft.Extensions.Caching.Memory;
using MemoizationFactory = Memoization.Memoization;

using var cache = new MemoryCache(new MemoryCacheOptions());

var options = new MemoryCacheEntryOptions
{
    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
    SlidingExpiration = TimeSpan.FromMinutes(5),
    Priority = CacheItemPriority.High,
};

var memoized = MemoizationFactory.Create<int, int>(x => x * x, cache, options);

Multi-Parameter Functions

using Microsoft.Extensions.Caching.Memory;
using MemoizationFactory = Memoization.Memoization;

using var cache = new MemoryCache(new MemoryCacheOptions());

static double Compute(double x, double y, int iterations)
    => Math.Pow(x + y, iterations % 10);

var memoized = MemoizationFactory.Create<double, double, int, double>(Compute, cache);
double result = memoized(3.14, 2.71, 1000);

Cache Size Limits

When SizeLimit is configured on MemoryCacheOptions, MemoryCache requires every entry to declare a Size:

using Microsoft.Extensions.Caching.Memory;
using MemoizationFactory = Memoization.Memoization;

using var cache = new MemoryCache(new MemoryCacheOptions
{
    SizeLimit = 1000,
    CompactionPercentage = 0.20,
});

var memoized = MemoizationFactory.Create<int, string>(
    x => x.ToString(),
    cache,
    new MemoryCacheEntryOptions { Size = 1 });

Shared Default Cache (Convenience)

For small scripts or single-instance applications, you can configure Memoization.DefaultCache once at startup and omit the cache argument:

using Microsoft.Extensions.Caching.Memory;
using MemoizationFactory = Memoization.Memoization;

// Process-wide mutable static state — prefer explicit IMemoryCache in DI apps and parallel tests.
MemoizationFactory.DefaultCache = new MemoryCache(new MemoryCacheOptions());

var memoized = MemoizationFactory.Create<int, int>(x => x * x);

Note: Memoization.DefaultCache is process-wide mutable static state. Prefer passing an explicit IMemoryCache instance in dependency-injected applications and parallel test suites to avoid cross-component or cross-test interference.

Scope & Async Guidance

Memoization.Net is designed for synchronous functions (Func<..., TResult>). Do not pass Task<TResult> or ValueTask<TResult> delegates to Memoization.Create:

  • Faulted-task cache poisoning: A delegate returning Task<TResult> returns a Task object synchronously, so a faulted or canceled Task is cached as a successful value and re-served to subsequent callers until evicted.
  • Single-consumption ValueTask hazards: ValueTask<TResult> must not be awaited more than once; caching and returning the same ValueTask<TResult> across calls violates its contract and causes undefined behavior.

For caching asynchronous I/O operations with stampede protection, use Microsoft.Extensions.Caching.Hybrid (HybridCache).

Cache Keys & Thread Safety

  • Cache keys include the input parameters and are scoped to the delegate returned by each Create call, so distinct memoized delegates can share an IMemoryCache without key collisions.
  • Parameter types should implement value-based Equals and GetHashCode and remain immutable while cached.
  • Cache operations inherit the thread safety of the underlying IMemoryCache (for example MemoryCache is thread-safe), though concurrent cache misses for the same key may invoke the underlying function more than once before the entry is populated.
  • If the wrapped function throws an exception, no cache entry is added and TrackLinkedCacheEntries expiration propagation on outer cache entries is not disrupted.

License

LGPL-3.0-or-later WITH LGPL-3.0-linking-exception. See LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 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.  net11.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.3.0 100 11/29/2026
1.2.1 283 8/22/2025
1.2.0-beta.6 180 8/10/2025
1.2.0-beta.3 160 8/22/2025
1.1.1 236 7/15/2025
1.1.0-beta.1.gb6ba33d 283 8/17/2022
1.0.0 11,369 1/3/2018

## [1.3.0] - 2026-11-29

### Fixed

- Scope cache keys per returned memoized delegate so distinct functions sharing an `IMemoryCache` do not collide on identical arguments.
- Apply `MemoryCacheEntryOptions` directly to the cache entry inside `GetOrCreate` without disrupting `TrackLinkedCacheEntries` expiration propagation on exceptions.

### Changed

- Aligned target frameworks to `netstandard2.0`, `netstandard2.1`, `net462`, `net10.0`, and `net11.0`.
- Added embedded Portable PDBs, SourceLink metadata, bounded package release notes, and shared license payloads.
- Enabled XML documentation generation and removed the local `SA0001` suppression in `Memoization.Generators`.
- Updated `README.md` and `DefaultCache` XML documentation to lead with explicit `IMemoryCache` usage, document `DefaultCache` as process-wide mutable static state, and clarify synchronous-only scope (recommending `HybridCache` for async I/O caching).

## [1.2.1]

### Changed

- Replaced T4 templates with the `Memoization.Generators` Roslyn incremental source generator.
- Re-licensed NuGet package to `LGPL-3.0-or-later WITH LGPL-3.0-linking-exception`.

Full changelog: https://github.com/hcoona/three/blob/main/src/public/lib/Memoization/CHANGELOG.md