Skip to content

About

Roslyn analyzer (SRC1000) that flags System.Runtime.Caching.MemoryCache in modern .NET and points you to Microsoft.Extensions.Caching.Memory (IMemoryCache). C# NuGet package.

Topics

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

81 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MemoryCache.Analyzers

NuGet Version Main build

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.

Example

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

Install the package in each project to analyze:

dotnet add package MemoryCache.Analyzers

Or add a package reference:

<PackageReference Include="MemoryCache.Analyzers"
                  Version="0.1.0"
                  PrivateAssets="all" />

Supported targets

  • 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.

Rules

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.

Configure severity

Use .editorconfig to change the severity of SRC1000:

[*.cs]
dotnet_diagnostic.SRC1000.severity = error

Guides

Build and test

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.runsettings

License

MemoryCache.Analyzers is licensed under the MIT License.

About

Roslyn analyzer (SRC1000) that flags System.Runtime.Caching.MemoryCache in modern .NET and points you to Microsoft.Extensions.Caching.Memory (IMemoryCache). C# NuGet package.

Topics

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages