Controls and Caching

What is Caching

 

Caching allows you to minimize the number of performed database queries. The server can store the data in memory and next time a user requests the content, the server returns content from memory instead of performing a resource-intensive database query. Caching can improve the performance of your Web site typically 10- to 100-times depending on your application.

 

The content expires after specified time span and must be retrieved from the database again. Each cached item has its name and the cache memory is common for all pages in your Web application.

 

Caching Support in Kentico CMS

 

You can manage the caching either by yourself in your code (please see the .NET Framework SDK documentation for more details) or you can leverage caching features of the following Kentico CMS Controls that are also used in CMS web parts:

 

CMSBreadCrumbs
CMSDataGrid
CMSDataList
CMSMenu
CMSRepeater
CMSSiteMap
CMSTabControl
CMSTreeMenu
CMSViewer
QueryDataGrid
QueryRepeater
QueryDataList

 

All of these controls offer the following properties, that allow you to set up caching:

 

Property Name

Description

Sample Value

CacheItemName

Name of the cache item the control will use.

"products_" & request.querystring("categoryid")

 

CacheMinutes

 

Number of minutes the retrieved content is cached for.

 

Zero indicates that the content will not be cached.

 

-1 indicates the site-level settings should be used.

 

10

 

 

Cache Expiration Time

 

By setting the CacheMinutes property to a value higher than zero, the control starts to cache its source data. You can configure caching for all Kentico CMS content using the Cache content parameter in Site Manager -> Settings -> Web site section. If you set any particular value to the CacheMinutes property of control/web part, it overrides the global settings. If you leave the value empty or set to -1 (minus one), the global settings apply.

 

The caching mechanism of Kentico CMS Controls uses absolute expiration time instead of sliding expiration. It means that the cache item expires after specified period of time regardless if it was requested or not. It ensures that content is updated from the database in a regular interval.

 

Overriding the site-level caching settings

 

If you need to cache most of the content on your web site, but still want to have a single control/web part that doesn't use cache, you can configure caching as described in the previous paragraph and set value 0 (zero) to the CacheMinutes property of the particular control. It will override the site-level settings and disable caching for the single control/web part.

 

Cache Item Name

 

It's important to understand the CacheItemName property: Since the cache is common for all pages in your application, the cache item name should be unique not only for all pages, but also inside one page (for case you use several Kentico CMS Controls with caching on one page).

 

When you leave the CacheItemName property empty, the control automatically generates a name in this form:

URL including parameters|control ID

 

If the content displayed on the page depends on some parameter, such as URL parameter or role of the current user, you need to adjust the CacheItemName value accordingly.

 

Example:

Your page products.aspx displays products according to the category that is passed through the URL parameter "category". You will need to use a code like this to ensure that the content will be cached "per category":

 

[C#]

 

CMSDataGrid.CacheItemName = "products_grid1_" + Request.QueryString["category"];

 

[VB.NET]

 

CMSDataGrid.CacheItemName = "products_grid1_" & Request.QueryString("category")