How to cache data in a MVC application

后端 未结 14 1275
鱼传尺愫
鱼传尺愫 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:09

    I have used it in this way and it works for me. https://msdn.microsoft.com/en-us/library/system.web.caching.cache.add(v=vs.110).aspx parameters info for system.web.caching.cache.add.

    public string GetInfo()
    {
         string name = string.Empty;
         if(System.Web.HttpContext.Current.Cache["KeyName"] == null)
         {
             name = GetNameMethod();
             System.Web.HttpContext.Current.Cache.Add("KeyName", name, null, DateTime.Noew.AddMinutes(5), Cache.NoSlidingExpiration, CacheitemPriority.AboveNormal, null);
         }
         else
         {
             name = System.Web.HttpContext.Current.Cache["KeyName"] as string;
         }
    
          return name;
    
    }
    

提交回复
热议问题