AsyncMemoryCache 4.0.2

dotnet add package AsyncMemoryCache --version 4.0.2
NuGet\Install-Package AsyncMemoryCache -Version 4.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AsyncMemoryCache" Version="4.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AsyncMemoryCache --version 4.0.2
#r "nuget: AsyncMemoryCache, 4.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AsyncMemoryCache as a Cake Addin
#addin nuget:?package=AsyncMemoryCache&version=4.0.2

// Install AsyncMemoryCache as a Cake Tool
#tool nuget:?package=AsyncMemoryCache&version=4.0.2

Build Version Coverage CodeFactor Downloads License

AsyncMemoryCache

A highly configurable cache that aims to improve upon IMemoryCache without relying on it as its backing store.
  • Lightweight
  • Strongly typed
  • Configurable eviction behavior
  • Configurable lifetime for cache entries
  • Lazy construction of cache object
  • Supports asynchronous factories
  • Automatic disposal of expired cache entries
  • (Optional) Integration with Microsoft.Extensions.DependencyInjection
  • (Optional) Integration with Microsoft.Extensions.Logging

Usage

using AsyncMemoryCache;
using AsyncMemoryCache.Extensions;

ILoggerFactory loggerFactory = ...;

var cacheLogger = loggerFactory.CreateLogger<AsyncMemoryCache<string, TheClassToCache>>();

var cacheConfig = new AsyncMemoryCacheConfiguration<string, TheClassToCache>
{
	EvictionBehavior = EvictionBehavior.Default // new DefaultEvictionBehavior(TimeProvider.System, TimeSpan.FromSeconds(45))
};

var cache = AsyncMemoryCache<string, TheClassToCache>.Create(cacheConfig, cacheLogger); // Logger is optional

// The factory is started here, will not block
var cacheEntityReference = cache.GetOrCreate("theKey", async () =>
{
	var createdObject = await ...;
	await Task.Delay(1000);
	return createdObject;
})
.WithSlidingExpiration(TimeSpan.FromHours(12));

// Will block here until the object is created
var theCachedObject = await cache["theKey"]; // Short-hand for await cache["theKey"].CacheEntity.ObjectFactory;

Extending the lifetime of a cached object

Given that
  • A cache item exists that is expired
  • Above mentioned cache item is currently referenced by some code

The way you deal with this is by using (calling Dispose() on) the CacheEntityReference returned whenever the cache object is accessed As long as at least one CacheEntityReference is alive (i.e. not disposed and still in-scope) the underlying cached object will not be evicted/disposed

// Create a cache entry that expires in 15 seconds
var cacheEntityReference = cache.GetOrCreate("theKey", async () =>
{
	var createdObject = await ...;
	await Task.Delay(1000);
	return createdObject;
})
.WithAbsoluteExpiration(DateTimeOffset.UtcNow.AddSeconds(15));

// do stuff with cache entry that takes longer than 15 seconds

	...

// The underlying cached object is not immediately disposed here, but is now eligible for disposal later on by eviction behaviors (if enabled)
cacheEntityReference.Dispose();
Product 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. 
.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 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.2 84 3/16/2024
4.0.1 63 3/16/2024
4.0.0 74 3/16/2024
3.4.0 60 3/9/2024
3.3.1 60 3/9/2024
3.3.0 62 3/9/2024
3.2.0 67 3/9/2024
3.1.0 90 2/16/2024
3.0.0 70 2/14/2024
2.0.2 78 2/12/2024
2.0.1 65 2/12/2024
2.0.0 60 2/9/2024
1.1.0 62 2/9/2024
1.0.10 61 2/4/2024
1.0.9 58 2/4/2024
1.0.8 60 2/4/2024
1.0.7 55 2/4/2024
1.0.6 55 2/4/2024
1.0.5 68 1/29/2024
1.0.4 54 1/29/2024
1.0.3 60 1/29/2024
1.0.2 54 1/29/2024
1.0.1 53 1/29/2024