TyKonKet
TyKonKet

Reputation: 314

Caching portions of page in MVC 5

At //Build/2016, Daniel Roth, during ASP.NET Core Deep Dive into MVC talked about TagHelpers and showed Cache TagHelper. This tag, among other things, allow to cache several portions of the page.

In my project (MVC5) i need to cache some pages, but i can't cache all the page, because in Layout View i have some data about users that can't be cached.

Here is an example of what i need to do:

<html>
    <head>
        <!--- Head code ---->
    <head>
    <body>
        <div id="page">

            <!--- This shouldn't be cached ---->
            <div id="top-menu">
                <!--- User Data ---->
            </div>

            <!--- This should be cached ---->
            <div id="page-content">
                <!--- Page Data ---->
            </div>

        </div>
    </body>
</html>

There's a way to do something like this even in MVC5?

Upvotes: 1

Views: 899

Answers (1)

Slicksim
Slicksim

Reputation: 7172

From a quick read through of that, it sounds like you need Donut Caching. This is a method of providing caching for portions of a page rather than all of it.

If you have separated your partials into actions, you can use the outputcache directive to cache these so they aren't executed each time.

Here is a good write up on the technique:

http://www.adamriddick.com/2013/06/asp-net-mvc4-donut-caching-donut-hole-caching/

Quite an old article, however, I believe the technique still holds water.

Upvotes: 2

Related Questions