How to cache data in a MVC application

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

    Reference the System.Web dll in your model and use System.Web.Caching.Cache

        public string[] GetNames()
        {
          string[] names = Cache["names"] as string[];
          if(names == null) //not in cache
          {
            names = DB.GetNames();
            Cache["names"] = names;
          }
          return names;
        }
    

    A bit simplified but I guess that would work. This is not MVC specific and I have always used this method for caching data.

提交回复
热议问题