Microsoft.Extensions.Caching.Memory 8.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Microsoft.Extensions.Caching.Memory --version 8.0.0
NuGet\Install-Package Microsoft.Extensions.Caching.Memory -Version 8.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="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.Extensions.Caching.Memory --version 8.0.0
#r "nuget: Microsoft.Extensions.Caching.Memory, 8.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 Microsoft.Extensions.Caching.Memory as a Cake Addin
#addin nuget:?package=Microsoft.Extensions.Caching.Memory&version=8.0.0

// Install Microsoft.Extensions.Caching.Memory as a Cake Tool
#tool nuget:?package=Microsoft.Extensions.Caching.Memory&version=8.0.0

About

Provides implementations for local and distributed in-memory cache. It stores and retrieves data in a fast and efficient way.

Key Features

  • A concrete implementation of the IMemoryCache interface, which represents a local in-memory cache that stores and retrieves data in a fast and efficient way
  • A distributed cache that supports higher scale-out than local cache
  • Expiration and eviction policies for its entries
  • Entry prioritization for when the cache size limit is exceeded and needs to be compacted by entry eviction
  • Track of cache statictics

How to Use

Use Microsoft.Extensions.Caching.Memory over System.Runtime.Caching when working with ASP.NET Core as it provides better integration support. For example, IMemoryCache works natively with ASP.NET Core dependency injection.

Local in-memory serialization:

using Microsoft.Extensions.Caching.Memory;

using MemoryCache cache = new(new MemoryCacheOptions());

object valueToCache = new();
string key = "key";

using (ICacheEntry entry = cache.CreateEntry(key))
{
    // Entries are committed after they are disposed therefore it does not exist yet.
    Console.WriteLine($"Exists: {cache.TryGetValue(key, out _)}\n");

    entry.Value = valueToCache;
    entry.SlidingExpiration = TimeSpan.FromSeconds(2);
}

bool exists = cache.TryGetValue(key, out object? cachedValue);
Console.WriteLine($"Exists: {exists}" );
Console.WriteLine($"cachedValue is valueToCache? {object.ReferenceEquals(cachedValue, valueToCache)}\n");

Console.WriteLine("Wait for the sliding expiration...");
Thread.Sleep(TimeSpan.FromSeconds(2));

Console.WriteLine("Exists: " + cache.TryGetValue(key, out _));

// You can also use the acceleration extensions to set and get entries
string key2 = "key2";
object value2 = new();

cache.Set("key2", value2);

object? cachedValue2 = cache.Get(key2);
Console.WriteLine($"cachedValue2 is value2? {object.ReferenceEquals(cachedValue2, value2)}");

Main Types

The main types provided by this library are:

  • Microsoft.Extensions.Caching.Memory.MemoryCache
  • Microsoft.Extensions.Caching.Memory.MemoryCacheOptions
  • Microsoft.Extensions.Caching.Distributed.MemoryDistributedCache
  • Microsoft.Extensions.Caching.Memory.MemoryDistributedCacheOptions

Additional Documentation

Microsoft.Extensions.Caching.Abstractions

Feedback & Contributing

Microsoft.Extensions.Caching.Memory is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 (2.2K)

Showing the top 5 NuGet packages that depend on Microsoft.Extensions.Caching.Memory:

Package Downloads
Microsoft.EntityFrameworkCore The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Entity Framework Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. EF Core works with SQL Server, Azure SQL Database, SQLite, Azure Cosmos DB, MySQL, PostgreSQL, and other databases through a provider plugin API. Commonly Used Types: Microsoft.EntityFrameworkCore.DbContext Microsoft.EntityFrameworkCore.DbSet

Microsoft.ApplicationInsights.PerfCounterCollector The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Application Insights Performance Counters Collector allows you to send data collected by Performance Counters to Application Insights. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156

Microsoft.AspNetCore.Mvc.Razor The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

ASP.NET Core MVC Razor view engine for CSHTML files.

Microsoft.AspNetCore.Mvc.TagHelpers The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

ASP.NET Core MVC default tag helpers. Contains tag helpers for anchor tags, HTML input elements, caching, scripts, links (for CSS), and more. This package was built from the source code at https://github.com/aspnet/Mvc/tree/a6199bbfbab05583f987bae322fb04566841aaea

Microsoft.AspNetCore.Mvc The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

ASP.NET Core MVC is a web framework that gives you a powerful, patterns-based way to build dynamic websites and web APIs. ASP.NET Core MVC enables a clean separation of concerns and gives you full control over markup. This package was built from the source code at https://github.com/aspnet/Mvc/tree/a6199bbfbab05583f987bae322fb04566841aaea

GitHub repositories (255)

Showing the top 5 popular GitHub repositories that depend on Microsoft.Extensions.Caching.Memory:

Repository Stars
jellyfin/jellyfin
The Free Software Media System
dotnet/efcore
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
App-vNext/Polly
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.
abpframework/abp
Open Source Web Application Framework for ASP.NET Core. Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET and the ASP.NET Core platforms. Provides the fundamental infrastructure, production-ready startup templates, application modules, UI themes, tooling, guides and documentation.
aspnetboilerplate/aspnetboilerplate
ASP.NET Boilerplate - Web Application Framework
Version Downloads Last updated
9.0.0-preview.4.24266.19 941 5/21/2024
9.0.0-preview.3.24172.9 39,238 4/11/2024
9.0.0-preview.2.24128.5 32,391 3/12/2024
9.0.0-preview.1.24080.9 59,108 2/13/2024
8.0.0 30,502,214 11/14/2023
8.0.0-rc.2.23479.6 727,784 10/10/2023
8.0.0-rc.1.23419.4 218,974 9/12/2023
8.0.0-preview.7.23375.6 174,254 8/8/2023
8.0.0-preview.6.23329.7 126,348 7/11/2023
8.0.0-preview.5.23280.8 113,608 6/13/2023
8.0.0-preview.4.23259.5 112,323 5/16/2023
8.0.0-preview.3.23174.8 151,700 4/11/2023
8.0.0-preview.2.23128.3 173,081 3/14/2023
8.0.0-preview.1.23110.8 127,894 2/21/2023
7.0.0 103,552,101 11/7/2022
7.0.0-rc.2.22472.3 216,711 10/11/2022
7.0.0-rc.1.22426.10 224,966 9/14/2022
7.0.0-preview.7.22375.6 99,449 8/9/2022
7.0.0-preview.6.22324.4 68,702 7/12/2022
7.0.0-preview.5.22301.12 62,492 6/14/2022
7.0.0-preview.4.22229.4 73,694 5/10/2022
7.0.0-preview.3.22175.4 74,158 4/13/2022
7.0.0-preview.2.22152.2 84,175 3/14/2022
7.0.0-preview.1.22076.8 65,298 2/17/2022
6.0.2-mauipre.1.22102.15 11,462 2/15/2022
6.0.2-mauipre.1.22054.8 18,078 1/19/2022
6.0.1 185,969,609 2/8/2022
6.0.0 89,999,632 11/8/2021
6.0.0-rc.2.21480.5 431,955 10/12/2021
6.0.0-rc.1.21451.13 199,783 9/14/2021
6.0.0-preview.7.21377.19 90,387 8/10/2021
6.0.0-preview.6.21352.12 47,173 7/14/2021
6.0.0-preview.5.21301.5 176,021 6/15/2021
6.0.0-preview.4.21253.7 84,179 5/24/2021
6.0.0-preview.3.21201.4 94,586 4/8/2021
6.0.0-preview.2.21154.6 67,813 3/11/2021
6.0.0-preview.1.21102.12 362,528 2/12/2021
5.0.0 162,430,169 11/9/2020
5.0.0-rc.2.20475.5 374,293 10/13/2020
5.0.0-rc.1.20451.14 320,637 9/14/2020
5.0.0-preview.8.20407.11 198,084 8/25/2020
5.0.0-preview.7.20364.11 115,417 7/21/2020
5.0.0-preview.6.20305.6 58,307 6/25/2020
5.0.0-preview.5.20278.1 6,190 6/10/2020
5.0.0-preview.4.20251.6 201,829 5/18/2020
5.0.0-preview.3.20215.2 114,564 4/23/2020
5.0.0-preview.2.20160.3 109,360 4/2/2020
5.0.0-preview.1.20120.4 61,962 3/16/2020
3.1.32 3,548,577 12/13/2022
3.1.31 701,072 11/8/2022
3.1.30 862,607 10/11/2022
3.1.29 1,019,990 9/13/2022
3.1.28 737,615 8/9/2022
3.1.27 806,931 7/12/2022
3.1.26 668,789 6/14/2022
3.1.25 1,084,571 5/10/2022
3.1.24 738,813 4/11/2022
3.1.23 1,364,998 3/8/2022
3.1.22 4,063,818 12/14/2021
3.1.21 3,204,613 11/7/2021
3.1.20 1,754,896 10/11/2021
3.1.19 2,479,169 9/14/2021
3.1.18 5,183,677 8/10/2021
3.1.17 2,210,613 7/13/2021
3.1.16 3,179,214 6/8/2021
3.1.15 3,328,009 5/11/2021
3.1.14 4,187,128 4/6/2021
3.1.13 5,149,951 3/9/2021
3.1.12 3,659,132 2/9/2021
3.1.11 6,151,447 1/12/2021
3.1.10 11,448,572 11/9/2020
3.1.9 15,979,414 10/13/2020
3.1.8 29,111,041 9/8/2020
3.1.7 15,426,784 8/11/2020
3.1.6 16,609,156 7/14/2020
3.1.5 16,859,616 6/9/2020
3.1.4 19,631,203 5/12/2020
3.1.3 29,939,760 3/24/2020
3.1.2 14,931,726 2/18/2020
3.1.1 23,289,780 1/14/2020
3.1.0 38,398,573 12/3/2019
3.1.0-preview3.19553.2 160,800 11/13/2019
3.1.0-preview2.19525.4 31,719 11/1/2019
3.1.0-preview1.19506.1 702,151 10/15/2019
3.0.3 299,013 2/18/2020
3.0.2 93,739 1/14/2020
3.0.1 2,102,958 11/18/2019
3.0.0 34,290,706 9/23/2019
3.0.0-rc1.19456.10 46,011 9/16/2019
3.0.0-preview9.19423.4 1,319,015 9/4/2019
3.0.0-preview8.19405.4 391,262 8/13/2019
3.0.0-preview7.19362.4 90,120 7/23/2019
3.0.0-preview6.19304.6 170,698 6/12/2019
3.0.0-preview5.19227.9 625,531 5/6/2019
3.0.0-preview4.19216.2 22,336 4/18/2019
3.0.0-preview3.19153.1 210,644 3/6/2019
3.0.0-preview.19074.2 31,645 1/29/2019
3.0.0-preview.18572.1 51,691 12/3/2018
2.2.0 139,727,410 12/3/2018
2.2.0-preview3-35497 240,581 10/17/2018
2.2.0-preview2-35157 169,204 9/12/2018
2.2.0-preview1-35029 100,281 8/22/2018
2.1.23 12,250,927 10/13/2020
2.1.2 31,325,358 8/21/2018
2.1.1 44,825,182 6/18/2018
2.1.0 61,210,278 5/29/2018
2.1.0-rc1-final 249,017 5/6/2018
2.1.0-preview2-final 238,832 4/10/2018
2.1.0-preview1-final 388,619 2/26/2018
2.0.2 10,506,467 5/7/2018
2.0.1 16,656,467 3/13/2018
2.0.0 100,052,821 8/11/2017
2.0.0-preview2-final 131,412 6/28/2017
2.0.0-preview1-final 1,041,040 5/10/2017
1.1.2 7,719,082 5/9/2017
1.1.1 15,952,398 3/6/2017
1.1.0 8,596,063 11/16/2016
1.1.0-preview1-final 69,534 10/24/2016
1.0.2 3,991,298 3/6/2017
1.0.1 622,161 12/12/2016
1.0.0 207,803,435 6/27/2016
1.0.0-rc2-final 275,541 5/16/2016
1.0.0-rc1-final 507,339 11/18/2015