AsyncMemoryCache 4.0.2

There is a newer version of this package available.
See the version list below for details.
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.1.0 37 11/13/2024
4.0.2 118 3/16/2024
4.0.1 79 3/16/2024
4.0.0 90 3/16/2024
3.4.0 81 3/9/2024
3.3.1 86 3/9/2024
3.3.0 80 3/9/2024
3.2.0 88 3/9/2024
3.1.0 105 2/16/2024
3.0.0 87 2/14/2024
2.0.2 93 2/12/2024
2.0.1 85 2/12/2024
2.0.0 77 2/9/2024
1.1.0 81 2/9/2024
1.0.10 87 2/4/2024
1.0.9 79 2/4/2024
1.0.8 80 2/4/2024
1.0.7 75 2/4/2024
1.0.6 73 2/4/2024
1.0.5 82 1/29/2024
1.0.4 67 1/29/2024
1.0.3 73 1/29/2024
1.0.2 68 1/29/2024
1.0.1 67 1/29/2024