For local, in-memory caching, PostSharp offers two backend classes:
MemoryCachingBackend relies on MemoryCache. It is included in the PostSharp.Patterns.Caching package and is available on all supported target frameworks.
MemoryCacheBackend relies on
Microsoft.Extensions.Caching.Memory.IMemoryCache. It is included in the PostSharp.Patterns.Caching.IMemoryCache package and requires .NET Standard 2.0 or later.
In-memory caching with MemoryCache
To use the MemoryCache class to store cached values in memory, assign an instance of the MemoryCachingBackend class to the DefaultBackend property.
CachingServices.DefaultBackend = new MemoryCachingBackend();
By default, the Default instance is used. To use another instance of the MemoryCache class, pass it to the constructor of the MemoryCachingBackend class.
MemoryCache cache = new MemoryCache( "myCache" );
CachingServices.DefaultBackend = new MemoryCachingBackend( cache );
See MSDN for details on the MemoryCache class.
In-memory caching with IMemoryCache
To store cached values in an instance that implements the Microsoft.Extensions.Caching.Memory.IMemoryCache interface:
Add a reference to the PostSharp.Patterns.Caching.IMemoryCache package.
Assign an instance of the MemoryCacheBackend class to the DefaultBackend property. Pass the
IMemoryCacheto the constructor.IMemoryCache cache = new MemoryCache(new MemoryCacheOptions()); CachingServices.DefaultBackend = new MemoryCacheBackend(cache);
See Cache in-memory in ASP.NET Core on MSDN for more information on the use of IMemoryCache.