Caching the data of page components
Many pages contain components (web parts or controls) that load and display data from the Kentico database, or other external sources. For example, when displaying a page with a list of news articles, the system retrieves text from the fields of news documents stored in the database, and then formats the data on the page.
Communication with storage spaces and processing of data are common weak points in the performance of pages. Content caching helps the system maximize the efficiency of data. The content cache saves data loaded by page components into the application’s memory. Components then re-use the cached data on future requests.
The following types of web parts and controls support content caching:
- Dedicated data sources
- Repeaters and viewers with built-in data sources
- Navigation components
Configuring content caching
You can set up content caching on two levels:
- Globally for entire sites (all data components on the given site)
- For individual instances of web parts or controls
We recommend caching all possible content. You can ensure that components do not display outdated content by setting cache dependencies. Most non-custom data sources have default dependencies that automatically clear the content cache whenever the stored data is modified.
To enable content caching globally:
- Open the Settings application.
- Select the System -> Performance category.
- Type a number of minutes into the Cache content (minutes) setting. The value determines how long the content cache retains data, and must be greater than 0.
- Save the settings.
With content caching enabled globally, the system caches the structured data of all page components by default.
To enable content caching for individual web part instances:
- Open the Pages application.
- Edit the page containing the web part on the Design tab.
- Configure the web part instance (double-click).
- Type a number of minutes into the Cache minutes property (in the System settings category). The value determines how long the cache stores the web part’s data, and must be greater than 0.
- (Optional) Add dependencies via the Cache dependencies property.
- Click OK to save the web part’s properties.
The system caches the data loaded by the given web part instance.
To disable content caching for specific web parts when content caching is enabled globally:
- Configure the web part instance on the Design tab.
- Type 0 (zero) into the Cache minutes property.
- Click OK to save the web part’s properties.
The web part instance reloads data from the data source without caching.
Setting dependencies for content cache
Cache dependencies allow the system to automatically clear cached data when related objects are modified. Web parts with data sources provide default dependencies for the content cache, and you can also add your own custom dependencies for individual instances:
Open the Pages application.
Edit the page containing the web part on the Design tab.
Configure the web part instance (double-click).
Add the dependencies into the Cache dependencies property (in the System settings category). For more information and examples, see Setting cache dependencies.
If you leave the Use default cache dependencies box checked, the system automatically clears the web part’s content cache:
- Whenever the loaded data changes (depends on the web part)
- When you modify the property configuration of the given web part instanceDefault dependencies do NOT cover data loaded via external or custom data sources. For example, custom query data sources only clear the content cache when the query itself changes, not the loaded data.
Click OK.
The system deletes the web part’s content cache whenever the specified objects change. With the cache cleared, the web part reloads the data from the source the next time a visitor opens the page.
Sharing the content cache between components
The content cache stores the data loaded by page components (web parts or controls) under cache keys. By default, the system generates a unique cache key name for each component. The default name contains variables such as the web part ID, the name of the user viewing the page, or the code name of the language selected for the page.
If you have multiple web part instances that load exactly the same data, you can share the cached content:
- Open the Pages application.
- Configure one of the web parts on the Design tab.
- Enter a custom key name into the Cache item name property (in the System settings category).
- Copy the key name into the Cache item name property of all web part instances that load the same data.
When a visitor opens a page containing one of the web parts, the system loads the data and saves it into the cache under the specified key. While the cached content is valid, other web parts to which you assigned the same Cache item name retrieve the data directly from the cache. This setup optimizes loading of content from the database (or other source) and avoids redundant keys in the cache.
Tip: You can use macro expressions to create dynamic cache item names based on variables such as query string parameters, or other context data.
Caching the page output of web parts
In addition to content caching, web parts also support Partial output caching. The partial cache stores the full HTML output code of web part instances.
See Caching portions of the page output for more information.
Partial caching has the following advantages and disadvantages when compared with content caching:
- Better efficiency — the partial cache allows the system to directly send the web part’s HTML output to the browser, without loading data or processing the web part at all
- Usable by all web parts with visible output, not just web parts that load structured data
- Not suitable for web parts whose output changes very frequently (for example lists with filtering)
- No default dependencies on the data loaded by web parts — you need to set custom partial cache dependencies to ensure that the content of web parts is up-to-date
Note: Set up partial caching for the web part that actually renders the content on the page (this may not always be the same web part as the data source).