Cache tips & tricks part 1

David te Kloese

I think we call all agree on the fact that cache can improve the performance of your website significantly. Kentico has lots of features doing the caching for you. But some things you'll have to set yourself. Nothing to worry about as Kentico made it easy for you to do!

Cache is a storage that allows faster access to previously requested data so its ready for future requests.

The two main benefits are:

  • Quicker loading of data (instead of a usually slower Webservice, SQL or file system call)
  • Reduction of processing for the same page requests.

In other words the 'lazier' the code (since it has less to do) the faster your site becomes.


So you've went through the Kentico Settings module and on the System > Performance group you've enabled output and partial caching. So you're done now right?
Kentico Output cache settings

If only it where that simple. If you've had the time to read the Information icons you could see that you still need to enable caching for a particular page or web part.

Kentico Output cache info box

Set cache

Kentico offers different types of Cache, but I'll currently talk about the Partial Output Cache and the full page Output Cache.
  • Content suitable for output cache are actually all pages that don't have any 'dynamic' parts. For example a contact details page (without a form).
  • Content suitable for partial cache are pieces that don't have 'dynamic' parts of all other pages not suitable for output cache. For example the web part containing the site menu.

I say dynamic between quotes since they can of course still change, but not as often as for example a Twitter-feed.

Output Cache (full page)

Page output cache needs to be set on the pages directly. All pages on deeper levels can inherit these settings from their parent.

Depending on your settings it will look something like this:
Setting full page output caching per page

Enable or inherit the 'use output cache' setting and set a number of minutes after which you want it to expire. The first request to this page will now create a cache item and the page is cached!

Partial Output Cache

When you want to cache just part of the specific page you can also use Partial output cache on web parts.

Simply go to settings of the Web Part (either by using the Design tab in the Pages Module or the Template Module).

Scroll down to the Performance section and enter a number in the Partial cache minutes text box.

Setting partial output caching per web part
I'll talk about dependencies later

From now on also the part of the page this web part renders will be cached!

But does it work

So that's fine and all but does is really help?

I've created an empty sample page with a repeater on it displaying
  • NodeID
  • first 3 characters of the NodeName
  • Random order
  • Top 500 pages

Because of the random order this normally seems like dynamic content so not something you'd want to cache. But in my case it would make it more easy to see if the result is really cached.

Since you cache the complete output you can't use the Kentico debug tools. For my example I used the Firebug add-on for Firefox. But most developer browser tools have a similar feature. You'll want a tool to show you the network loading times and size per request and break it down per type or request.

So without cache I got the following first request:
Firebug plugin shows Page Loading time not cached version

And the second request:
Firebug plugin shows Page Loading time cached version

So besides a lesson in Dutch you can see that it improved the loading time from over a second to almost none. It is still the same size, just over 13KB and the random data seems in the same order.

Do you want even more proof?

You can also see the caching itself happen.

Go into the Kentico Settings module and on the System > Debug > Cache access group enable the debug. Never do this on production!

(Don't forget to uncheck the Disable debugging setting on the top of this settings page). Enable Cache output debug

Now if you make a first request to our page (clear the cache first) you'll see a list on the bottom of the page. This is rendered by Kentico and shows you the Cache additions or requests.

Cache addition debug

I've altered it a bit so it's easier to see, but the number 5 is created by our repeater and has an addition to the Cache.

Inspect the cache

In the previous image you also see the cache key used to store the date. If you go to the Debug module and search for the key itself in the Cache Items you can see more information about this cache key. As you can see in the image below you can explore what objects are in the cache, whats the size and also when it's expiring.

Inspect Cache

If you click the Eye icon you can even see the data itself. You might also notice that although I now use caching the cached data itself is over 1.1 MB. Since it's just to show 500 ID's and Node names this seems a bit much. If you look closely you see the cache contains a table with 500 rows and over 300 columns!

So remember just caching everything isn't the only solution! Always think about what data you're needing.

Simply by adding the NodeID and NodeName to the Columns field in the Repeater I got it down to 60kB!

Inspect smaller Cache
There are some extra columns needed by the Repeater or the Cache settings.

In the next parts I'll continue about Cache dependencies, Caching in custom code and more...