NTDLS.FastMemoryCache Usage
A thread-safe partitioned memory cache for C# with max-size management and performance tracking.
NTDLS.FastMemoryCache
What it is. A fast, thread-safe, partitioned in-memory cache for C#. It was built to handle caches holding large numbers of items - the partitioning removes the lock contention you hit with a single global cache - and lets you enforce a maximum memory size and track cache performance. Configuration options cover max memory size, scavenge rate and partition count.
When to reach for it. Your cache holds thousands or more entries under concurrent access, or you need a hard cap on how much memory it can grow to. Skip it if you are only caching a few dozen items; IMemoryCache from the framework is simpler and perfectly adequate there.
Getting started
Instantiate the cache, then use TryGet / Upsert around your expensive operation. Here is a file-read cache:
private readonly PartitionedMemoryCache _cache = new();
public string ReadFileFromDisk(string path)
{
// The key could be derived from the path, e.g. path.GetHashCode().ToString().
var cacheKey = path;
if (_cache.TryGet<string>(cacheKey, out var cachedObject))
{
return cachedObject;
}
string fileContents = File.ReadAllText(path);
// Upsert takes an estimated size in bytes so the cache can enforce its max.
_cache.Upsert(cacheKey, fileContents, fileContents.Length * sizeof(char));
return fileContents;
}
The size argument you pass to Upsert is what makes the max-memory constraint work - report each value's approximate byte footprint and the cache scavenges when the total gets too large. If you need tighter control, pass a configuration object at construction time to set the max memory size, scavenge rate and partition count.
NuGet: NTDLS.FastMemoryCache