ASP.NET Core中的缓存
作者:互联网
依赖注入
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddMemoryCache())
.Build();
获取服务
- 通过IServiceProvider
IMemoryCache cache =
host.Services.GetRequiredService<IMemoryCache>();
- 构造函数依赖注入
public class MyClass{
private readonly IMemoryCache _cache;
public MyClass(IMemoryCache cache){
_cache=cache;
}
}
参考
https://docs.microsoft.com/zh-cn/dotnet/core/extensions/caching
标签:Core,ASP,cache,Extensions,IMemoryCache,MyClass,using,NET,Microsoft 来源: https://www.cnblogs.com/wuhailong/p/16493826.html