AsyncMemoryCache 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AsyncMemoryCache --version 2.0.0                
NuGet\Install-Package AsyncMemoryCache -Version 2.0.0                
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="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AsyncMemoryCache --version 2.0.0                
#r "nuget: AsyncMemoryCache, 2.0.0"                
#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=2.0.0

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

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
  • Integration with Microsoft.Extensions.DependencyInjection
  • Integration with Microsoft.Extensions.Logging

Usage

using AsyncMemoryCache;

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 = new AsyncMemoryCache<string, TheClassToCache>(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;
});

cacheEntityReference.CacheEntity.WithSlidingExpiration(TimeSpan.FromHours(12));

// Will block here until the object is created
var theCachedObject = 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

var cacheEntityReference = cache.GetOrCreate("theKey", async () =>
{
	var createdObject = await ...;
	await Task.Delay(1000);
	return createdObject;
});

// Object expires in 15 seconds
cacheEntityReference.CacheEntity.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 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. 
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 52 11/13/2024
4.0.2 119 3/16/2024
4.0.1 80 3/16/2024
4.0.0 91 3/16/2024
3.4.0 82 3/9/2024
3.3.1 87 3/9/2024
3.3.0 81 3/9/2024
3.2.0 89 3/9/2024
3.1.0 106 2/16/2024
3.0.0 88 2/14/2024
2.0.2 94 2/12/2024
2.0.1 86 2/12/2024
2.0.0 78 2/9/2024
1.1.0 82 2/9/2024
1.0.10 88 2/4/2024
1.0.9 80 2/4/2024
1.0.8 81 2/4/2024
1.0.7 76 2/4/2024
1.0.6 74 2/4/2024
1.0.5 83 1/29/2024
1.0.4 68 1/29/2024
1.0.3 74 1/29/2024
1.0.2 69 1/29/2024
1.0.1 68 1/29/2024