Setting cache dependencies

Cache dependencies allow the application to automatically clear cached data when related objects are modified.

The system uses dummy cache keys to create dependencies between cached data and other objects. Dummy keys are cache items without any data that represent objects or groups of objects. When an object is modified, the system “touches” the corresponding dummy keys, which causes the cache to delete all items that depend on the given dummy keys.

Sample scenario

A page with the /Products alias path displays a list of products (pages), which are placed as child items in the content tree. The product data is cached, for example using caching in the page’s MVC code or the content caching functionality of a Repeater web part. The cached data has a dependency on the node|corporatesite|/products|childnodes dummy key, which the system creates automatically.

When a visitor requests the Products page, the product data for the list is loaded from the database and stored in the content cache. If a child page of the Products page is modified, the dummy key is touched, and the cache deletes the dependent data.

The following table shows which dummy cache keys are touched when objects are modified:

Object type

Touched dummy keys

Sample dummy key values

Pages
(content tree nodes)

node|<site name>|<alias path>|<culture>
node|<site name>|<alias path>
nodeid|<node id>
nodeid|<linked node id>
documentid|<document id>
documentid|<document id>|attachments
nodes|<site name>|<page type code name>|all
nodeguid|<site name>|<node guid>
nodeorder (touched when the order of any page in the content tree is changed)

+ for all ancestors of the modified page:
node|<site name>|<alias path>|childnodes

node|corporatesite|/home|en-us
node|corporatesite|/home
nodeid|12
nodeid|34
documentid|39
documentid|39|attachments
nodes|corporatesite|cms.menuitem|all
nodeguid|corporatesite|a58ed488-5545-48d0- …
nodeorder

node|corporatesite|/|childnodes

Objects
(not pages)

<object type>|all
<object type>|byid|<id>
<object type>|byname|<code name>
<object type>|byguid|<guid>

Tip: You can find the object type values in the System application on the Object types tab.

cms.user|all
cms.user|byid|53
cms.user|byname|administrator
cms.user|byguid|1ced44f3-f2fc- …

Media files

mediafile|<guid>
mediafile|preview|<guid>

mediafile|1ced44f3-f2fc- …
mediafile|preview|1ced44f3-f2fc- …

Metafiles

metafile|<guid>

metafile|1ced44f3-f2fc- …

Page attachments

cms.attachment|all
documentid|<attachment document id>
documentid|<attachment document id>|attachments
attachment|<guid>

cms.attachment|all
documentid|32
documentid|32|attachments
attachment|1ced44f3-f2fc- …

Forum attachments

forumattachment|<guid>

forumattachment|1ced44f3-f2fc- …

Page relationships

nodeid|<node id>|relationships

nodeid|5|relationships

Settings

cms.settingskey|<settings key code name>
(touched when the global value of the setting is changed)

cms.settingskey|<site id>|<settings key code name>
(touched when the site-specific value of the setting is changed)

Tip: You can find the setting key code name values in the Modules application by editing any module and opening the Settings tab.

cms.settingskey|cmspagedescriptionprefix
cms.settingskey|1|cmspagedescriptionprefix

Avatars

avatarfile|<guid>

avatarfile|1ced44f3-f2fc- …

Page templates

template|<id>

template|12

Custom table data records

customtableitem.<custom table code name>|all
customtableitem.<custom table code name>|byid|<id>

customtableitem.customtable.sampletable|all
customtableitem.customtable.sampletable|byid|2

Dependencies on form data records

The system currently does not touch any dummy cache keys when changes occur for data submitted via forms.

As a workaround, developers can prepare custom event handlers for BizFormItemEvents. Use the handler method to touch a custom cache key, and then enter the key into your cache dependencies. For example:




private void FormItem_InsertAfterHandler(object sender, BizFormItemEventArgs e)
{
    // Touches a custom cache key, for example: "customformdata|bizform.contactus"
    CacheHelper.TouchKey("customformdata|" + e.Item.BizFormClassName);
}


Adding custom cache dependencies

You can set custom cache dependencies for the following types of cache:

Create the dependencies by typing the names of dummy cache keys into the appropriate Cache dependencies property. Each dummy key must be on a separate line when entering dependencies in the web part properties dialog. The system automatically clears the related cache when the specified dummy keys are touched (i.e. when the corresponding objects are modified).

Examples

Scenario: You have an MVC page displaying a list of articles, and the code used to retrieve the article data uses the CacheHelper.Cache method for caching. You need to clear the cached article data whenever one of the article pages is modified.

Solution: When preparing the CacheSettings parameter in the code, add the nodes|<site name>|<article page type code name>|all dummy key as a cache dependency. The system touches this dummy key whenever any article page of the given type is updated.

Scenario: You have a web part displaying information about users. You need to clear the web part’s partial output cache whenever one of the user’s data is modified.

Solution: Add the cms.user|all dummy key into the web part’s Partial cache dependencies property. The system touches this dummy key whenever the data of a user in the system changes.

Scenario: You have a web part displaying information about one specific user — the administrator (UserID is 53, UserGuid is 849711D0-739D-412E-92B6-FE40EDCADC4A). You need to clear the web part’s partial output cache when the administrator’s user account data is modified.

Solution: Add any of the following dummy keys into the web part’s Partial cache dependencies property:

  • cms.user|byid|53
  • cms.user|byname|administrator
  • cms.user|byguid|849711D0-739D-412E-92B6-FE40EDCADC4A

Scenario: You have a web part displaying a list of news articles. The web part loads the data from all CMS.News pages on the website. The site’s code name is NewsSite. You need to clear the web part’s partial output cache when any of the news pages is updated.

Solution: Add the nodes|newssite|cms.news|all dummy key into the web part’s Partial cache dependencies property.