ASP.NET MVC3 Caching

ASP.NET MVC3 builds on the caching abilities found in previous versions of ASP.NET and adds partial page output caching to your toolbox. Partial page output caching allows you to cache the rendering of a specific action on your view to enable your site to run faster. This can be a great technique to avoid unnecessary database calls to reduce server load and decrease client latency.

Rendering a Partial View

Razor allows you to encapsulate portions of your page in partial views that are roughly analogous to the ASCX user controls in classic ASP.NET. Your partial view needs to define a controller action similar to the controller actions of your normal page actions with the key difference that you'll be returning a PartialViewResult object instead of a generic ActionResult.

There is one parameter in the code example below that we use to build a string including that parameter along with the current time. The controller returns the string using the PartialView helper method to generate the PartialViewResult object.

public PartialViewResult CachableAction(string SomeParameter)
{
string returnValue = String.Format("Parameter:{0}<br>Time:{1}", 
SomeParameter, 
DateTime.Now.Ticks.ToString());
return PartialView("CachableAction", returnValue);
}

Right click on the partial view name defined in your controller and choose "Add View". In the dialog that appears, check the box next to "Create as a partial view". The resulting cshtml file generated will be completely blank instead of including the normal pieces you'd see when creating a blank page.

The view in this example renders the string returned from the controller along with a message.

@model string
<p>This is from my CachableAction</p> 
@Html.Raw(Model)

You call your partial action in your main page by using the @Html.Action razor helper method. This method allows you to call your partial action and have it run inline on the page. You can use this method anywhere including layout pages and even inside of other partial views.

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        <p>This is the page.</p>
        @Html.Action("CachableAction", "Home", new { SomeParameter="Yes" })
        @Html.Action("CachableAction", "Home", new { SomeParameter="No" })
    </div>
</body>
</html>

Adding Caching to Your Partial View

You can add caching to any of your controller actions by adding the [OutputCache] attribute to your methods. The OutputCache has some implied functionality you need to be aware of. B If your controller action has any parameters, like the "SomeParameter" in this example, and if the value varies between calls, it will cache a separate result for each parameter.

You must always define the Duration parameter when you use the OutputCache attribute. Duration is the amount of time in seconds that the cached result is good for. Be sure to choose something reasonable based on your application's constraints. If, for example, you're caching a list of categories that will only change rarely, it's probably fine to set your duration to be several hours long. If it's something like a product information page that might change more frequently, you might set the caching duration to less than a minute.

Although the cache will automatically vary by the parameters defined on your controller action, you can override this behavior by setting the VaryByParam option. VaryByParam will cache your control based on the values defined in the HTTP GET parameters or the form fields defined in this list. You can specify multiple parameters by separating each one with a semicolon.

[OutputCache(Duration=60,VaryByParam="ParamA;ParamB;")]
public PartialViewResult CachableAction(string SomeParameter)
{
...
}

VaryByCustom

VaryByCustom allows you to define custom key/value options to vary your caching by. You can do this by overriding GetVaryByCustomString in your Global.asax.cs file. This function will be called any time VaryByCustom is called. ASP.NET's cache will look at the string returned by this function and if it matches a value already in its cache, that is the value it will use. In the example below, if custom has "Browser" as the VaryByCustom value and the user's browser is "AOL" then it will return the string "AOL" as the value.

public override string GetVaryByCustomString(HttpContext context,  string custom)
{
if(custom=="Browser")
{
If(context.Request.Browser.AOL) { return "AOL"; }
else { return "NotAOL"; }
}
return Guid.NewGuid().ToString();
}

Across your entire application, you only have one place to define your GetVaryByCustomString method so you need to be careful to insure there isn't any unintended caching where it isn't desired. One way to insure this is to return a GUID in string form if none of your custom strings was matched, so your view won't be cached if it isn't supposed to be cached.

To call your GetVaryByCustomString in your global.asax.cs file, add a VaryByCustom parameter to your controller's action method.

[OutputCache(Duration=60,VaryByCustom="Browser")]
public PartialViewResult CachableAction(string SomeParameter)
{
...
}

Restrictions on Partial View Caching Options

Not all of the options available on the OutputCache attribute are valid to use on partial actions. Page-wide types such as Location, VaryByHeader, NoStore, SqlDependency, CacheProfile and VaryByEncoding will result in errors if you attempt to use them.

Security

It is very important to keep in mind that the cache is shared among all of the users of your application. This means if your partial view renders data specific to User A, User B will see User A's data unless you have some specific mechanism in place to keep them separated such as the SessionID. In general, however, it's not very efficient to cache anything on a per-user basis. The amount of server resources consumed per-user could quickly get out of control. As a result of this consideration, it's highly recommended to only cache things that are not user specific.

Conclusion

ASP.NET MVC3's partial views are powerful and efficient ways to add additional modularity to your web application. Adding the ability to cache these views can result in a dramatic reduction in latency and server resources if used correctly.



Related Articles

Comments

  • Si te gusta el bajo extra, bien, esa es tu elección

    Posted by cheneason on 06/04/2013 09:50pm

    [url=http://www.beats-by-dre-2013.webstarts.com/]auriculares beats[/url] At Pressekonference afholdt med bestyrelsesmedlemmer i selskabet og pressen medlemmer, erklærede direktøren for selskabet, at statistikken over salget efter indførelsen af den nye beats har krydset benchmarks. Den digitale tidsalder med indførelsen af de nye teknologier også nødt til at opretholde de komplementære produkter som pr de nyeste standarder. Dette er, hvad kræver en opgradering i teknologien. Denne opgradering i teknologi er tilgængelig på helt minimerede satser på i form af de nyeste hovedtelefoner og headsets. Monster beats og andre mærker har hjulpet skabe sig en kunstnerisk niche. [url=http://www.cascos-beats-baratos.webnode.fr/]Cascos beats baratos[/url] One caveat: The Zoro does not hold up well to repeated high volume usage on deep bass tracks that cause distortion. As with many pairs in the $100-range, this eventually seems to lead to some permanent driver damage. It doesn’t mean the headphones will be unusable, but they might suffer from a bit of rattling or fuzziness on certain frequencies, regardless of the volume—at the end of our testing, a third deep bass, high volume test ended up doing some damage to the right ear’s drivers. [url=http://www.beatsbydrdrebaratos.weebly.com/]auriculares beats[/url] Det er officielt. Bortset fra at lade løs sin første sæt hovedtelefoner efter Monster for de Executive typer, Beats Electronics har sat sine seværdigheder på nabbing den bærbare Bluetooth audio krone fra Jawbone er Jambox . Du husker dette rammer FCC lidt siden, men i dag er Dr. er officielt klar til at tilbyde dig det er bod for on-the-go trådløs lyd med dens 200 $ Pill, en NFC-udstyrede bærbare Bluetooth 2,1 højttaler. Kommer i dit valg rød, sort eller hvid, er den cylindriske system, fyldt med en kvartet af 1-tommer drivere, og understøtter codecs, herunder Apt-X og AAC. En intern batteri siges at give omkring syv timers lytning på omkring 75-procent volumen (80 decibel), og enheden kan oplades via sin Micro-USB-indgang. Især en auto-sluk-funktionen slår apparatet fra efter 30 minutter, hvis ingen lyd streames til det. Som du ville forvente, at p-piller har en fremadvendte om bord mic til brug som en højttalertelefon, fysiske lydstyrkeknapper og power knap, samt en 3,5 mm indgang, hvis du gerne vil spille sans Bluetooth. Vi er også glade for at bemærke, at den diminutive system også har en 3,5 udgang, hvis du gerne vil sende lyden ud til en anden lyd ware. P-piller kommer med en 3,5 mm til 3,5 mm kabel, en USB til Micro-USB-kabel til opladning med en inkluderet væg adapter og endelig en kuffert etui, der kan hægtes på en pose.

    Reply
  • Hi..

    Posted by Amit on 07/02/2012 06:10am

    Hi, Thanks for sharing good information, well I am using the output cache like OutputCache(Duration=60) but at some places I want invalidate this cache and want to put updated value in cache. I did try with remove cache and some other way but unfortunately i am not able to update the cached values. Please help me... Thanks in advanced.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds