High-performance yet easy to use memory manager implementation in .NET.
✔ Super FAST and low memory usage. 🔥
✔ Support for joint execution for GetOrSetAsync methods, so only 1 runs concurrently. 🔥
✔ Support for IMemoryCache interface.
✔ Dependency Injection ready.
✔ Support for tags.
✔ Support for keyless items.
✔ Support for persistent items.
✔ Developers friendly ❤️ Easy to use.
Method | Mean | Allocated | Speedup |
---|---|---|---|
MemoryCore_Add | 35.76 ns | 80 B | 4.8x |
Microsoft_Add | 170.81 ns | 104 B | |
MemoryCore_Get | 10.91 ns | - | 2.4x |
Microsoft_Get | 26.39 ns | - | |
MemoryCore_Exists | 10.98 ns | - | 2.4x |
Microsoft_Exists | 26.47 ns | - | |
MemoryCore_Remove | 18.50 ns | - | 2.2x |
Microsoft_Remove | 41.52 ns | - |
Install MemoryCore with NuGet:
Install-Package MemoryCore
Or via the .NET Core command line interface:
dotnet add package MemoryCore
then register the required services easily:
services.AddMemoryCore();
or create an instance:
using var cache = new MemoryCoreManager();
public class Example
{
private readonly IMemoryCore cache;
public Example(IMemoryCore cache)
{
this.cache = cache;
}
public void AddCacheItem()
{
cache.Add("key", "value", TimeSpan.FromMinutes(1));
}
public string GetCacheItem()
{
return cache.Get<string>("key");
}
public string GetOrAddItem()
{
return cache.TryGetOrAdd("key",
() => "value",
TimeSpan.FromMinutes(1));
}
public async Task<string> GetOrAddItemAsync()
{
return await cache.TryGetOrAddAsync("key",
async () => await GetValueAsync(),
TimeSpan.FromMinutes(1));
}
private async Task<string> GetValueAsync()
{
await Task.Delay(1000); //simulate action
return "value";
}
}
Please feel free to PR. I highly appreciate any contribution!