Kentico CMS 6.0 Controls

Caching

Caching

Previous topic Next topic Mail us feedback on this topic!  

Caching

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

What is Caching

 

Caching allows you to minimize the number of performed database queries. The server can store the data in the application's memory and the next time a user requests the content, the server will return it from the memory instead of performing a resource-intensive database query. Caching can improve the performance of your website significantly, depending on the specifics of your application.

 

The content expires after the 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:

 

Controls in the CMS Controls -> Navigation section

Controls in the CMS Controls -> Listings and viewers section

CMSPageManager

CMSSearchResults

 

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

 

Property Name

Description

Sample Value

CacheDependencies

List of the cache keys on which the cached data depends.

cms.user|all

CacheItemName

Name of the cache item the control will use.

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

 

CacheMinutes

Number of minutes the retrieved content is cached for.

"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 (minutes) setting in the Site Manager -> Settings -> System -> Performance section. If you set any particular value to the CacheMinutes property of a control/web part, it overrides the global settings. If you leave the value empty or set it 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 a specified period of time regardless of if it was requested or not. It ensures that content is updated from the database at a regular interval.

 

Overriding the site-level caching settings

 

If you need to cache most of the content on your website, 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 the 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 (in 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 a URL parameter or the 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 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")

 

Cache dependencies

 

Using the CacheDependencies property, you can specify which object changes cause the control/web part's cache to get cleared. Below, you can find a table showing which dummy cache keys get touched when some object gets changed, including some examples. By entering the appropriate dummy keys, one per line, you can specify that when the object gets changed, the cache gets cleared.

 

If you leave the property empty, default settings will be used. The default settings are configured for each control and include all possible object changes that the content of the web part could depend on.

 

Object

Touched keys

Sample values

Document

(TreeNode)

node|<sitename>|<aliaspath>|<culture>

node|<sitename>|<aliaspath>

nodeid|<nodeid>

nodeid|<linkednodeid>

nodes|<sitename>|<classname>|all

+ for every parent node:

node|<sitename>|<aliaspath>|childnodes

node|corporatesite|/home|en-us

node|corporatesite|/home

nodeid|12

nodeid|34

nodes|corporatesite|cms.menuitem|all

 

node|sitename|/|childnodes

Any object

(except documents)

<classname>|all

<classname>|byid|<id>

<classname>|byname|<codename>

<classname>|byguid|<guid>

cms.user|all

cms.user|byid|53

cms.user|byname|administrator

cms.user|byguid|1ced44f3-f2fc- ...

Metafile

metafile|<guid>

metafile|1ced44f3-f2fc- ...

Document attachment

attachment|<guid>

attachment|1ced44f3-f2fc- ...

Forum attachment

forumattachment|<guid>

forumattachment|1ced44f3-f2fc- ...

Avatar

avatarfile|<guid>

avatarfile|1ced44f3-f2fc- ...

Media file

mediafile|<guid>

mediafile|preview|<guid>

mediafile|1ced44f3-f2fc- ...

mediafile|preview|1ced44f3-f2fc- ...

Page template

template|<id>

template|12

CacheHelper

.ClearFullPageCache

fullpage

fullpage

 

Example:

 

1. Let's presume that you have a control/web part displaying some information about users. Therefore, whenever some user gets their details modified, the control/web part's cache should be cleared. To ensure this, you need to enter cms.user|all into the property, which is the dummy key that would get touched whenever some user's details get changed.

 

2. Now let's presume that your control/web part is displaying information about one particular user - the administrator. Her user name is administrator, her ID is 53 and her GUID is something beginning with 1ced44f3-f2fc. So if you want to have the cache cleared whenever this user's details are changed, you can use any of the following three keys that specify the user by the previously named properties:

 

cms.user|byid|53

cms.user|byname|administrator

cms.user|byguid|1ced44f3-f2fc-...