MemoryCache.Analyzers is a Roslyn analyzer that identifies creation of
System.Runtime.Caching.MemoryCache in modern .NET projects. Microsoft's
ASP.NET Core caching guidance
recommends Microsoft.Extensions.Caching.Memory over that compatibility API.
SRC1000 reports the MemoryCache object creation in this C# code:
using System;
using System.Runtime.Caching;
MemoryCache cache = new("orders"); // SRC1000
cache.Set("order:42", "ready", DateTimeOffset.UtcNow.AddMinutes(5));Use Microsoft.Extensions.Caching.Memory instead:
using System;
using Microsoft.Extensions.Caching.Memory;
using MemoryCache cache = new(new MemoryCacheOptions());
cache.Set("order:42", "ready", TimeSpan.FromMinutes(5));Install the package in each project to analyze:
dotnet add package MemoryCache.AnalyzersOr add a package reference:
<PackageReference Include="MemoryCache.Analyzers"
Version="0.1.0"
PrivateAssets="all" />- Language: C#
- Analyzed applications: .NET Standard 2.0, .NET Core, and .NET 5 or later
- Excluded applications: .NET Framework
- Analyzer target framework:
netstandard2.0
SRC1000 reports only object creation for
System.Runtime.Caching.MemoryCache or a type derived from it. Calls such as
Get and Set, other member access, and MemoryCache.Default do not produce
diagnostics.
| ID | Category | Severity | Code fix | Title |
|---|---|---|---|---|
| SRC1000 | Compatibility | Warning | No | Avoid System.Runtime.Caching.MemoryCache in .NET Core+ |
See the rule reference for implementation details. See Guides for installation, configuration, and migration steps.
For experimental patterns and decorators built on IMemoryCache, see
memory-cache-solutions.
Use .editorconfig to change the severity of SRC1000:
[*.cs]
dotnet_diagnostic.SRC1000.severity = error- Install MemoryCache.Analyzers
- Configure MemoryCache.Analyzers
- Migrate from System.Runtime.Caching.MemoryCache
- GetOrCreate is not atomic: cache stampede in IMemoryCache
- Manual QA: try the local package in a sample project
- Architecture: analyzer flow and build and release flow
- Runbooks: releases, bad releases, and CI failures
The repository uses the .NET SDK version in global.json.
dotnet build MemoryCache.Analyzers.sln /p:PedanticMode=true
dotnet test MemoryCache.Analyzers.sln --no-build --settings ./build/targets/tests/test.runsettingsMemoryCache.Analyzers is licensed under the MIT License.