How to cache data in a MVC application

后端 未结 14 1401
鱼传尺愫
鱼传尺愫 2020-11-22 11:39

I have read lots of information about page caching and partial page caching in a MVC application. However, I would like to know how you would cache data.

In my scena

14条回答
  •  我在风中等你
    2020-11-22 12:17

    For .NET 4.5+ framework

    add reference: System.Runtime.Caching

    add using statement: using System.Runtime.Caching;

    public string[] GetNames()
    { 
        var noms = System.Runtime.Caching.MemoryCache.Default["names"];
        if(noms == null) 
        {    
            noms = DB.GetNames();
            System.Runtime.Caching.MemoryCache.Default["names"] = noms; 
        }
    
        return ((string[])noms);
    }
    

    In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. In previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes. In the .NET Framework 4, the System.Runtime.Caching namespace contains APIs that are designed for both Web and non-Web applications.

    More info:

    • https://msdn.microsoft.com/en-us/library/dd997357(v=vs.110).aspx

    • https://docs.microsoft.com/en-us/dotnet/framework/performance/caching-in-net-framework-applications

提交回复
热议问题