In this tutorial, you will learn all about the CachePartial() helper and how you can use it to level up your Umbraco websites performance. When building a website, performance matters. Page speed is usually improved incrementally through implementing a number of small tweaks. Caching all of the partial view requests is one of those potential small tweaks. The CachedPartial() helper can be thought of like a decorator that wraps the normal .NET Partial() helper. Instead of simply rendering out the contents of a partial view, CachedPartial() will also cache it for you. On any subsequent requests made for views wrapped in CachedPartial(), a cached version will be returned. This cached version will obviously make the page load quicker. The method signature for the CachedPartial(), looks like this:

Where <Name Of View> is the name of the partial view that you want to wrap, <View Model> is the model and <Cache Duration> is the time in seconds the helper will cache the partial for.

The helper uses the view name as the cache key. One quirk to be aware of is that the helper will work globally. Do not be surprised if you get unexpected results when trying to render the same partial view from different locations, for example, a separate document-type . The cached version will be returned until the cache duration is hit. Imagine you had two pages, One and Two that both call the same partial view.

The first time a request is made, the partial view will be rendered and its output will be cached. If page One was called first, the data passed into the partial from One will be rendered within Two. If document-type Two was called first then the data on Two would be rendered on page One. It's kind of like a race condition, the thing that gets hit first will determine what gets rendered. If you need page-specific caching you can use the cache-by-page override, like this:

Using this cacheByPage overload means that you can change the cache between different pages. The example above would render two different results rather than the first result that was called. Another thing to note is that this override only works on different pages. If you tried to render the same view on the same page twice, the first call is cached and that version will be returned on the second call, regardless of the cacheByPage override. It is possible to get this level of caching, however, you will need to write a custom helper. The code to do this is shown below:

This custom helper can then be used in a view file like the:

TIP Caching WILL only happens if you are NOT in debug mode.