Memoization.Net
1.3.0
dotnet add package Memoization.Net --version 1.3.0
NuGet\Install-Package Memoization.Net -Version 1.3.0
<PackageReference Include="Memoization.Net" Version="1.3.0" />
<PackageVersion Include="Memoization.Net" Version="1.3.0" />
<PackageReference Include="Memoization.Net" />
paket add Memoization.Net --version 1.3.0
#r "nuget: Memoization.Net, 1.3.0"
#:package Memoization.Net@1.3.0
#addin nuget:?package=Memoization.Net&version=1.3.0
#tool nuget:?package=Memoization.Net&version=1.3.0
Memoization.Net
A memoization library for .NET that caches function results in IMemoryCache to avoid redundant computations.
Features
- Built on
Microsoft.Extensions.Caching.Abstractions(IMemoryCache) - Supports functions with 1 to 16 parameters via source-generated
Memoization.Createoverloads - Supports a shared
Memoization.DefaultCacheor per-callIMemoryCacheinstances - Supports
MemoryCacheEntryOptions(expiration, priority, size limits) - Failed invocations throw without populating the cache
- Multi-targeting:
netstandard2.0,netstandard2.1,net462,net10.0, andnet11.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.DefaultCacheis process-wide mutable static state. Prefer passing an explicitIMemoryCacheinstance 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 aTaskobject synchronously, so a faulted or canceledTaskis cached as a successful value and re-served to subsequent callers until evicted. - Single-consumption
ValueTaskhazards:ValueTask<TResult>must not be awaited more than once; caching and returning the sameValueTask<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
Createcall, so distinct memoized delegates can share anIMemoryCachewithout key collisions. - Parameter types should implement value-based
EqualsandGetHashCodeand remain immutable while cached. - Cache operations inherit the thread safety of the underlying
IMemoryCache(for exampleMemoryCacheis 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
TrackLinkedCacheEntriesexpiration propagation on outer cache entries is not disrupted.
Links
License
LGPL-3.0-or-later WITH LGPL-3.0-linking-exception. See LICENSE for details.
| Product | Versions 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. |
-
.NETFramework 4.6.2
- Microsoft.Extensions.Caching.Abstractions (>= 2.1.0)
- System.ValueTuple (>= 4.4.0)
-
.NETStandard 2.0
- Microsoft.Extensions.Caching.Abstractions (>= 2.1.0)
-
.NETStandard 2.1
- Microsoft.Extensions.Caching.Abstractions (>= 2.1.0)
-
net10.0
- Microsoft.Extensions.Caching.Abstractions (>= 2.1.0)
-
net11.0
- No dependencies.
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