Caching 5.1.2
See the version list below for details.
dotnet add package Caching --version 5.1.2
NuGet\Install-Package Caching -Version 5.1.2
<PackageReference Include="Caching" Version="5.1.2" />
<PackageVersion Include="Caching" Version="5.1.2" />
<PackageReference Include="Caching" />
paket add Caching --version 5.1.2
#r "nuget: Caching, 5.1.2"
#:package Caching@5.1.2
#addin nuget:?package=Caching&version=5.1.2
#tool nuget:?package=Caching&version=5.1.2
Caching
<img src="https://github.com/jchristn/Caching/raw/main/assets/icon.png" height="128" width="128">
High-performance, thread-safe caching library for .NET with FIFO and LRU eviction policies, automatic expiration, persistence support, and comprehensive event notifications.
What Is This Library?
Caching is a lightweight, production-ready caching library that provides:
- FIFO (First-In-First-Out) Cache: Evicts the oldest entries when capacity is reached
- LRU (Least Recently Used) Cache: Evicts the least recently accessed entries
- Thread-Safe: All operations are fully thread-safe for concurrent access
- Automatic Expiration: Time-based expiration with sliding or absolute TTL
- Event Notifications: Comprehensive events for cache operations
- Persistence Layer: Optional persistence to disk or custom storage
- Statistics Tracking: Built-in hit/miss rates, eviction counts, and performance metrics
- Memory Limits: Optional memory-based eviction in addition to count-based
- Modern API: GetOrAdd, AddOrUpdate, and async-ready patterns
Installation
dotnet add package Caching
Or via Package Manager:
Install-Package Caching
Current Release
The current package version is 5.1.2. This patch makes every Try* method (TryGet, TryAddReplace, TryGetOrAdd, TryRemove) return false instead of throwing for any failure, including null keys, value factory exceptions, and persistence driver exceptions; only ObjectDisposedException is thrown after disposal. Version 5.1.1 keeps the expiration task running after persistence or Expired handler failures, stops synchronous GetOrAdd/AddOrUpdate from running factories, persistence, and event handlers under the cache lock, and makes ExpirationIntervalMs changes take effect immediately. Version 5.1.0 added built-in observability: metrics and traces through the standard .NET Meter and ActivitySource APIs (both named Caching), ready for Prometheus, Tempo, Grafana, or any OTLP backend. See Telemetry and TELEMETRY.md.
Quick Start
Basic FIFO Cache
using Caching;
// Create a FIFO cache with capacity of 1000, evicting 100 items when full
var cache = new FIFOCache<string, Person>(capacity: 1000, evictCount: 100);
// Add items
cache.AddReplace("user:123", new Person { Name = "Alice", Age = 30 });
// Get items
Person person = cache.Get("user:123");
// Try pattern (no exceptions)
if (cache.TryGet("user:123", out Person p))
{
Console.WriteLine($"Found: {p.Name}");
}
// Remove items
cache.Remove("user:123");
// Dispose when done
cache.Dispose();
Basic LRU Cache
// LRU evicts least recently accessed items
var cache = new LRUCache<string, byte[]>(capacity: 500, evictCount: 50);
cache.AddReplace("image:1", imageBytes);
cache.Get("image:1"); // Updates last-used timestamp
cache.Dispose();
Key Features
1. Expiration
Absolute Expiration
// Expires at specific time
cache.AddReplace("session:xyz", sessionData, DateTime.UtcNow.AddMinutes(30));
// Or use TimeSpan for relative expiration
cache.AddReplace("temp:data", tempData, TimeSpan.FromSeconds(60));
Sliding Expiration
// Enable sliding expiration (TTL refreshes on access)
cache.SlidingExpiration = true;
cache.AddReplace("sliding:key", value, TimeSpan.FromMinutes(5));
// Each time you access the item, expiration resets to 5 minutes from now
cache.Get("sliding:key"); // Refreshes expiration
2. GetOrAdd Pattern
// Atomically get existing or create new value
var person = cache.GetOrAdd("user:456", key =>
{
// This factory only runs if key doesn't exist
return database.GetPerson(456);
});
// With expiration
var data = cache.GetOrAdd("data:789",
key => LoadExpensiveData(key),
TimeSpan.FromHours(1));
3. AddOrUpdate Pattern
// Add if new, update if exists
var result = cache.AddOrUpdate(
"counter:visits",
addValue: 1,
updateValueFactory: (key, oldValue) => oldValue + 1);
Console.WriteLine($"Visit count: {result}");
4. Events
cache.Events.Added += (sender, e) => Console.WriteLine($"Added: {e.Key}");
cache.Events.Replaced += (sender, e) => Console.WriteLine($"Replaced: {e.Key}");
cache.Events.Removed += (sender, e) => Console.WriteLine($"Removed: {e.Key}");
cache.Events.Evicted += (sender, keys) => Console.WriteLine($"Evicted {keys.Count} items");
cache.Events.Expired += (sender, key) => Console.WriteLine($"Expired: {key}");
cache.Events.Cleared += (sender, e) => Console.WriteLine("Cache cleared");
cache.Events.Disposed += (sender, e) => Console.WriteLine("Cache disposed");
5. Persistence
Implement the IPersistenceDriver<TKey, TValue> interface (all methods are async):
public class FilePersistence : IPersistenceDriver<string, string>
{
private readonly string _directory;
public FilePersistence(string directory)
{
_directory = directory;
Directory.CreateDirectory(directory);
}
public async Task WriteAsync(string key, string data, CancellationToken ct = default)
{
await File.WriteAllTextAsync(Path.Combine(_directory, key), data, ct);
}
public async Task<string> GetAsync(string key, CancellationToken ct = default)
{
return await File.ReadAllTextAsync(Path.Combine(_directory, key), ct);
}
public async Task DeleteAsync(string key, CancellationToken ct = default)
{
await Task.Run(() => File.Delete(Path.Combine(_directory, key)), ct);
}
public async Task ClearAsync(CancellationToken ct = default)
{
foreach (var file in Directory.GetFiles(_directory))
await Task.Run(() => File.Delete(file), ct);
}
public async Task<bool> ExistsAsync(string key, CancellationToken ct = default)
{
return await Task.Run(() => File.Exists(Path.Combine(_directory, key)), ct);
}
public async Task<List<string>> EnumerateAsync(CancellationToken ct = default)
{
return await Task.Run(() => Directory.GetFiles(_directory)
.Select(Path.GetFileName)
.ToList(), ct);
}
}
// Use with cache
var persistence = new FilePersistence("./cache_data");
var cache = new LRUCache<string, string>(1000, 100, persistence);
// Restore from persistence on startup
await cache.PrepopulateAsync();
// All add/remove operations automatically persist
await cache.AddReplaceAsync("key", "value"); // Written to disk
await cache.RemoveAsync("key"); // Deleted from disk
// Sync methods also available (block on async internally)
cache.AddReplace("key2", "value2");
cache.Remove("key2");
6. Statistics
var cache = new FIFOCache<string, object>(1000, 100);
// Perform operations
cache.AddReplace("key1", "value1");
cache.Get("key1"); // Hit
cache.TryGet("missing", out _); // Miss
// Get statistics
var stats = cache.GetStatistics();
Console.WriteLine($"Hit Rate: {stats.HitRate:P}");
Console.WriteLine($"Hits: {stats.HitCount}");
Console.WriteLine($"Misses: {stats.MissCount}");
Console.WriteLine($"Evictions: {stats.EvictionCount}");
Console.WriteLine($"Expirations: {stats.ExpirationCount}");
Console.WriteLine($"Current Count: {stats.CurrentCount}");
Console.WriteLine($"Capacity: {stats.Capacity}");
// Reset counters
cache.ResetStatistics();
7. Memory Limits
var cache = new FIFOCache<string, byte[]>(10000, 100);
// Limit cache to 100MB
cache.MaxMemoryBytes = 100 * 1024 * 1024;
// Provide size estimator for your value type
cache.SizeEstimator = bytes => bytes.Length;
// Cache will evict entries if memory limit is exceeded
cache.AddReplace("large", new byte[10 * 1024 * 1024]); // 10MB
Console.WriteLine($"Memory used: {cache.CurrentMemoryBytes} bytes");
8. Configuration Options
var cache = new LRUCache<int, string>(1000, 100);
// Sliding expiration
cache.SlidingExpiration = true;
// Expiration check interval (default: 1000ms)
cache.ExpirationIntervalMs = 500;
// Memory limits
cache.MaxMemoryBytes = 50 * 1024 * 1024; // 50MB
cache.SizeEstimator = str => str.Length * 2; // Unicode estimation
9. Telemetry
Every cache emits metrics and traces through the BCL System.Diagnostics.Metrics.Meter and System.Diagnostics.ActivitySource, both named Caching. There is no dependency on OpenTelemetry or any exporter, and the cost is effectively zero until a collector subscribes.
var cache = new LRUCache<string, Product>(10000, 100, persistenceDriver);
cache.Name = "products"; // cache.name label; use a short static value
// In the host, subscribe a collector to the "Caching" meter and activity source, for example with Radiant:
settings.Sources.AddMeter(CacheTelemetryNames.MeterName);
settings.Sources.AddActivitySource(CacheTelemetryNames.ActivitySourceName);
// or with the OpenTelemetry SDK:
// .WithMetrics(m => m.AddMeter("Caching")).WithTracing(t => t.AddSource("Caching"))
What an operator gets:
- Metrics: duration and outcome of every operation, hit/miss counts, evictions by reason, expirations, persistence driver call latency and failures, atomic lock wait time and queue depth, entries/capacity/memory gauges, expiration sweep duration and last-success time, and a build info gauge.
- Traces: a span per mutation (
caching add_replace,caching get_or_add, ...) nested under the caller's span,stage:lock_waitandstage:value_factorychild spans, a client span per persistence call (persistence write, ...), and a root span per expiration sweep that removed entries.
Per-instance switches live on cache.Telemetry (Enable, EnableMetrics, EnableTraces, TraceLookups, RecordExceptionMessages). Keys and values are never recorded. See TELEMETRY.md for the full metrics and spans catalog, PromQL, alerts, and a suggested dashboard.
API Reference
All Try* methods follow one contract: they return false (with any out value set to default) instead of throwing for every failure, including invalid arguments, value factory exceptions, and persistence driver exceptions. The only exception they throw is ObjectDisposedException after the cache is disposed. Failures are still visible through telemetry. If the persistence driver fails, the in-memory change has already been applied, as with the throwing variant.
Core Methods
| Method | Description |
|---|---|
AddReplace(key, value, expiration?) |
Add or replace a cache entry |
AddReplaceAsync(key, value, expiration?, ct?) |
Async version of AddReplace |
Get(key) |
Get value (throws if not found) |
GetOrDefault(key, defaultValue?) |
Get value or return default if not found |
TryGet(key, out value) |
Try to get value (returns false if not found or on any failure) |
GetOrAdd(key, factory, expiration?) |
Get existing or add new value atomically |
GetOrAddAsync(key, asyncFactory, expiration?, ct?) |
Async version of GetOrAdd |
AddOrUpdate(key, addValue, updateFactory, expiration?) |
Add new or update existing value |
AddOrUpdateAsync(key, addValue, asyncUpdateFactory, expiration?, ct?) |
Async version of AddOrUpdate |
Remove(key) |
Remove entry |
RemoveAsync(key, ct?) |
Async version of Remove |
TryRemove(key, out value) |
Try to remove entry, returns removed value |
Contains(key) |
Check if key exists |
Clear() |
Remove all entries |
ClearAsync(ct?) |
Async version of Clear |
Count() |
Get current number of entries |
GetKeys() |
Get all keys |
All() |
Get all key-value pairs |
Oldest() |
Get key of oldest entry |
Newest() |
Get key of newest entry |
Prepopulate() |
Load from persistence layer |
PrepopulateAsync(ct?) |
Async version of Prepopulate |
GetStatistics() |
Get cache statistics |
ResetStatistics() |
Reset counters |
Constructors
// Basic cache
new FIFOCache<TKey, TValue>(capacity, evictCount);
new LRUCache<TKey, TValue>(capacity, evictCount);
// With custom key comparer
new FIFOCache<TKey, TValue>(capacity, evictCount, comparer: StringComparer.OrdinalIgnoreCase);
// With persistence
new FIFOCache<TKey, TValue>(capacity, evictCount, persistenceDriver);
// With persistence and custom comparer
new LRUCache<TKey, TValue>(capacity, evictCount, persistenceDriver, comparer);
Properties
| Property | Description |
|---|---|
Capacity |
Maximum number of entries |
EvictCount |
Number of entries to evict when full |
ExpirationIntervalMs |
How often to check for expired entries (ms) |
SlidingExpiration |
Enable sliding expiration |
MaxMemoryBytes |
Maximum memory limit (0 = unlimited) |
SizeEstimator |
Function to estimate value size |
CurrentMemoryBytes |
Current estimated memory usage |
HitCount |
Total cache hits |
MissCount |
Total cache misses |
EvictionCount |
Total evictions |
ExpirationCount |
Total expirations |
HitRate |
Cache hit rate (0.0 to 1.0) |
KeyComparer |
The equality comparer used for keys |
Events |
Event handlers |
Persistence |
Persistence driver |
Name |
Cache name used as the cache.name telemetry label (default default) |
Telemetry |
Per-instance telemetry settings (Enable, EnableMetrics, EnableTraces, TraceLookups, RecordExceptionMessages) |
Thread Safety
All cache operations are thread-safe and can be called concurrently from multiple threads:
var cache = new LRUCache<int, string>(10000, 100);
// Safe to call from multiple threads
Parallel.For(0, 1000, i =>
{
cache.AddReplace(i, $"value{i}");
cache.TryGet(i, out _);
if (i % 10 == 0) cache.TryRemove(i, out _);
});
Performance Tips
Choose the Right Cache Type:
- Use FIFO when access patterns don't matter (e.g., time-series data)
- Use LRU when recent items are more likely to be accessed again
Set Appropriate Capacity:
- Monitor
HitRateto tune capacity - Higher capacity = better hit rate but more memory
- Monitor
Tune EvictCount:
- Larger
EvictCount= fewer eviction operations but more items removed at once - Smaller
EvictCount= more frequent evictions but finer-grained
- Larger
Use TryGet for Optional Lookups:
TryGetis faster than catching exceptions fromGet
Minimize Event Handler Work:
- Events fire synchronously; keep handlers fast
- Offload heavy work to background tasks
Memory Limits:
- Only use
MaxMemoryBytesif needed; it adds overhead - Provide accurate
SizeEstimatorfor best results
- Only use
Migrating from v4.x to v5.0
v5.0 is a breaking change. Key updates required:
1. Persistence Driver (Async-Only)
// v4.x (sync methods)
public class MyDriver : IPersistenceDriver<string, string>
{
public void Write(string key, string data) { }
public string Get(string key) { }
public void Delete(string key) { }
public void Clear() { }
public bool Exists(string key) { }
public List<string> Enumerate() { }
}
// v5.0 (async methods)
public class MyDriver : IPersistenceDriver<string, string>
{
public Task WriteAsync(string key, string data, CancellationToken ct = default) { }
public Task<string> GetAsync(string key, CancellationToken ct = default) { }
public Task DeleteAsync(string key, CancellationToken ct = default) { }
public Task ClearAsync(CancellationToken ct = default) { }
public Task<bool> ExistsAsync(string key, CancellationToken ct = default) { }
public Task<List<string>> EnumerateAsync(CancellationToken ct = default) { }
}
2. TryRemove Returns Value
// v4.x
bool removed = cache.TryRemove("key");
// v5.0
bool removed = cache.TryRemove("key", out string value);
// or if you don't need the value:
bool removed = cache.TryRemove("key", out _);
3. New Features
// Custom key comparer
var cache = new FIFOCache<string, int>(100, 10,
comparer: StringComparer.OrdinalIgnoreCase);
// GetOrDefault (doesn't throw)
string value = cache.GetOrDefault("missing", "fallback");
// Async methods
await cache.AddReplaceAsync("key", "value");
var result = await cache.GetOrAddAsync("key", async k => await FetchAsync(k));
await cache.PrepopulateAsync();
Migrating from v3.x to v4.0
// v3.x
cache.Events.Added = handler; // Overwrites all handlers! ❌
// v4.0+
cache.Events.Added += handler; // Adds handler ✅
Contributing
Contributions are welcome! Please open an issue or PR on GitHub.
License
See LICENSE.md
| 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 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 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. |
| .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 was computed. 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. |
-
.NETStandard 2.0
- Microsoft.CSharp (>= 4.7.0)
- System.Diagnostics.DiagnosticSource (>= 10.0.12)
-
.NETStandard 2.1
- Microsoft.CSharp (>= 4.7.0)
- System.Diagnostics.DiagnosticSource (>= 10.0.12)
-
net10.0
- No dependencies.
-
net8.0
- System.Diagnostics.DiagnosticSource (>= 10.0.12)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Caching:
| Package | Downloads |
|---|---|
|
IpMatcher
Library for maintaining a match list of IP addresses and networks and comparing inputs to see if a match exists. |
|
|
SimpleUdp
SimpleUdp is a super simple way of building UDP clients and servers in C#. |
|
|
LiteGraph
LiteGraph is a property graph database with support for graph relationships, tags, labels, metadata, data, and vectors. |
|
|
View.Models
Database models, services, and supporting classes for for View AI. |
|
|
Verbex
Verbex is a comprehensive inverted index library that enables fast, scalable full-text search capabilities. |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Caching:
| Repository | Stars |
|---|---|
|
litegraphdb/litegraph
LiteGraph is a multi-modal AI data platform - a property graph with relational, vector, and MCP support, to power knowledge and AI persistence and retrieval
|
|
|
Guerra24/LRReader
A feature-complete reader and client for LANraragi
|
| Version | Downloads | Last Updated |
|---|---|---|
| 5.1.3 | 86 | 10/3/2026 |
| 5.1.2 | 490 | 10/3/2026 |
| 5.1.1 | 42 | 10/3/2026 |
| 5.1.0 | 45 | 10/3/2026 |
| 5.0.1 | 3,626 | 7/25/2026 |
| 5.0.0 | 12,267 | 12/29/2025 |
| 4.0.0 | 4,440 | 10/19/2025 |
| 3.1.3 | 12,801 | 1/7/2025 |
| 3.1.2 | 388 | 12/23/2024 |
| 3.1.1 | 38,064 | 1/9/2024 |
| 3.1.0 | 344 | 12/26/2023 |
| 3.0.1 | 1,401 | 10/1/2023 |
| 3.0.0 | 2,365 | 8/1/2023 |
| 2.0.1 | 477 | 7/31/2023 |
| 2.0.0.4 | 3,387 | 2/24/2023 |
| 2.0.0.2 | 5,559 | 5/18/2022 |
Patch release: Try methods (TryGet, TryAddReplace, TryGetOrAdd, TryRemove) now consistently return false instead of throwing for every failure except disposal. 5.1.1: the expiration task no longer stops after a persistence or Expired handler failure; synchronous GetOrAdd and AddOrUpdate no longer run factories, persistence, or event handlers under the cache lock; ExpirationIntervalMs changes take effect immediately.