Where should caching occur in an ASP.NET MVC application?

前端 未结 6 2171
有刺的猬
有刺的猬 2021-02-07 12:14

I\'m needing to cache some data using System.Web.Caching.Cache. Not sure if it matters, but the data does not come from a database, but a plethora of custom obj

6条回答
  •  面向向阳花
    2021-02-07 13:06

    Quick Answer

    I would start with CONTROLLER caching, use the OutputCache attribute, and later add Model caching if required. It's quicker to implement and has instant results.

    Detail Answer (cause i like the sound of my voice)

    Here's an example.

    [OutputCache(Duration=60, VaryByParam="None")]
    public ActionResult CacheDemo() {
      return View();
    }
    

    This means that if a user hits the site (for the cache requirements defined in the attribute), there's less work to get done. If there's only Model caching, then even though the logic (and most likely the DB hit) are cached, the web server still has to render the page. Why do that when the render result will always be the same?

    So start with OutputCaching, then move onto Model caching as you performance test your site.

    Output caching is also a lot simpler to start out with. You don't have to worry about web farm distributed caching probs (if you are part of a farm) and the caching provider for the model.

    Advanced Caching Techniques

    You can also apply donut caching -> cache only part of the UI page :) Check it out!

提交回复
热议问题