- Basic implementation of generic in-memory cache provider, based on Microsoft.Extensions.Caching.Memory.
- Comes with set of useful thread safe extensions to get objects from cache or add them with factory.
- Introduces several not supported by default methods, like
IEnumerable<string> GetAllKeys()orvoid DeleteAll(). - Supports tags on cache entries and eviction by tag.
- Mutating operations (
Add,Delete,EvictByTag,DeleteAlland async variants) are serialized to keep tag indexes consistent under concurrent use. - Read operations (
Get,HasKey,GetAllKeysand async variants) are lock-free for performance. This means reads may observe transient states during concurrent mutations, which is expected for a non-transactional cache.
Register in DI with any container of your preference. Microsoft.Extensions.DependencyInjection will be used in this sample.
services.AddSingleton<ICacheProvider, InMemoryCacheProvider>();
class Test(ICacheProvider cacheProvider)
{
void Foo(CancellationToken cancellationToken = default)
{
var cacheKey = "test";
var cacheTags = ["test1", "test2"];
var valueFromCache = cacheProvider.GetOrAdd(cacheKey,
CacheValueFactory,
tags: cacheTags,
cacheCreationCondition: s => !string.IsNullOrEmpty(s),
expirationOptions: new ExpirationOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1),
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(10),
SlidingExpiration = TimeSpan.FromSeconds(30)
},
cancellationToken: cancellationToken);
}
private string CacheValueFactory() => return "test value";
}