High performance, thread-safe in-memory caching primitives for .NET.
- ConcurrentLru, a lightweight pseudo LRU based on the 2Q eviction policy. Also with time based eviction.
- ConcurrentLfu, an approximate LFU based on the W-TinyLFU admission policy.
- Configurable atomic valueFactory to mitigate cache stampede.
- Configurable thread-safe wrappers for IDisposable cache values.
- A builder API to easily configure cache features.
- SingletonCache for caching single instance values, such as lock objects.
- High performance concurrent counters.
Please refer to the wiki for full API documentation, and a complete analysis of hit rate, latency and throughput.
BitFaster.Caching is installed from NuGet:
dotnet add package BitFaster.Caching
ConcurrentLru is a light weight drop in replacement for ConcurrentDictionary, but with bounded size enforced by the TU-Q eviction policy (derived from 2Q). There are no background threads, no global locks, concurrent throughput is high, lookups are fast and hit rate outperforms a pure LRU in all tested scenarios.
Choose a capacity and use just like ConcurrentDictionary, but with bounded size:
int capacity = 128;
var lru = new ConcurrentLru<string, SomeItem>(capacity);
var value = lru.GetOrAdd("key", (key) => new SomeItem(key));ConcurrentLfu is a drop in replacement for ConcurrentDictionary, but with bounded size enforced by the W-TinyLFU eviction policy. ConcurrentLfu has near optimal hit rate and high scalability. Reads and writes are buffered then replayed asynchronously to mitigate lock contention.
Choose a capacity and use just like ConcurrentDictionary, but with bounded size:
int capacity = 128;
var lfu = new ConcurrentLfu<string, SomeItem>(capacity);
var value = lfu.GetOrAdd("key", (key) => new SomeItem(key));Use WithWeigher to bound the cache by total weight instead of item count:
using BitFaster.Caching;
using BitFaster.Caching.Lfu;
var cache = new ConcurrentLfuBuilder<string, byte[]>()
.WithCapacity(1_000_000)
.WithWeigher(new ByteArrayWeightCalculator())
.Build();
sealed class ByteArrayWeightCalculator : IWeightCalculator<string, byte[]>
{
public int GetWeight(string key, byte[] value) => value.Length;
}The calculator must be thread-safe and return non-negative weights. Weights are calculated on creation and replacement, not when a value is mutated in place. Entries heavier than the budget are not retained. Zero-weight entries are exempt from capacity eviction, so Count can exceed Capacity. As with count-based eviction, the budget is enforced asynchronously.
WithWeigher composes with WithEvents, WithExpireAfterWrite, WithExpireAfterAccess, and WithExpireAfter. Scoped and atomic value wrappers are not supported.