Asp.Net Core: Use memory cache outside controller

前端 未结 3 512
轻奢々
轻奢々 2021-01-02 03:14

In ASP.NET Core its very easy to access your memory cache from a controller

In your startup you add:

public void ConfigureServices(IServiceCollection         


        
3条回答
  •  臣服心动
    2021-01-02 03:36

    I am bit late here, but just wanted to add a point to save someone's time. You can access IMemoryCache through HttpContext anywhere in application

    var cache = HttpContext.RequestServices.GetService();
    

    Please make sure to add MemeoryCache in Startup

    services.AddMemoryCache();
    

提交回复
热议问题