Asp.Net Core: Use memory cache outside controller

前端 未结 3 503
轻奢々
轻奢々 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:45

    There is another very flexible and easy way to do it is using System.Runtime.Caching/MemoryCache

    System.Runtime.Caching/MemoryCache:
    This is pretty much the same as the old day's ASP.Net MVC's HttpRuntime.Cache. You can use it on ASP.Net CORE without any dependency injection, in any class you want to. This is how to use it:

    // First install 'System.Runtime.Caching' (NuGet package)
    
    // Add a using
    using System.Runtime.Caching;
    
    // To get a value
    var myString = MemoryCache.Default["itemCacheKey"];
    
    // To store a value
    MemoryCache.Default["itemCacheKey"] = myString;
    

提交回复
热议问题