- Sitecore Cookbook for Developers
- Yogesh Patel
- 819字
- 2021-07-16 11:15:01
Improving site performance by caching renderings
Sitecore provides you with a feature to cache the output of presentation components to maximize performance and throughput by applying caching settings on renderings or sublayouts. It is also known as Output Cache or HTML Cache.
In this recipe, we will implement different static and dynamic output caching provided by Sitecore based on its usage.
Getting ready
In this recipe, we will consider that you have already created different components such as Breadcrumb, Title and Body, and Footer renderings. You can understand that the Footer will have the same output on all the pages for any device, while Breadcrumbs will get changed on every page. Also, chances are that we have content pages that can vary the output based on query strings or device types. We will apply caching on these renderings.
How to do it…
We will first apply caching settings to the Breadcrumb rendering and publish it:
- Select the Breadcrumb rendering from the Content Editor. In the field editor pane, go to the Caching section. You will find the following options:
- Tick the Cacheable and Vary By Data checkboxes and save the rendering item. From the ribbon, publish it in the Publish group in the Publish tab. Make sure that you have published required templates, content items, renderings, and media items.
- Now, when you view any published page in normal mode, which will serve content from the web database (or from the content delivery server), Sitecore will cache the Breadcrumb rendering output for individual data sources (here, for every page) and will serve for all the subsequent requests from the cache itself.
- You can apply the same settings for Title and Body, Side Menu, and other renderings as well.
- If we apply caching to any rendering with only Cacheable ticked, it will create a common cache for all the pages as we have not configured it with any varying by criteria. Such a setting can be useful for static content of websites, which remains the same wherever used, such as the Footer renderings.
How it works…
Sitecore provides you with caching options that allow the rendered data to be retrieved from the cache if the data source, device, authentication status, user, rendering parameters, and/or query string parameters are the same as the previous request. Sitecore creates different caches for each variation of cache setting that we set.
The following table shows you different Vary By caching parameters and their uses, which will be helpful in deciding caching criteria:

Each output cache consists of a list of any number of key-value pairs. Each cache key is a unique string identifying a presentation component and applies various caching variations. There is no option available such as Vary By language as each cache key automatically includes the context language.
We can also check how many renderings are cached, how frequently they are getting used, how much time they take to serve cached data, and other details using Sitecore's own tool, stats.aspx
, as shown in the following image. You can access this tool with http://sitecorecookbook/Sitecore/admin/stats.aspx
.

Each Sitecore site has a configurable maximum size for the output cache, which is called HTML cache. You can check the usage of caches using a cache page, as shown in the following screenshot, which you can access using http://sitecorecookbook/Sitecore/admin/cache.aspx
:

The preceding screenshot says that the website site has a total of eight different HTML caches generated that occupy a total of 3.12MB and its capacity is to store 50MB. The value 7.9KB shows the difference between the last fetched occupied cache size and current occupied cache size.
Tip
For high-volume websites, use the fewest caching criteria if possible to minimize memory usage. You can also increase the HTML cache size, which you will learn in the Improving the performance of Sitecore instances recipe in Chapter 11, Securing, Scaling, Optimizing, and Troubleshooting.
You should avoid using output caching for a rendering when:
- It has a form postback mechanism. (If caching is applied, postbacks won't work.)
- It pulls frequent content from external services or serves time-based content. (It will cache external content once and serve older content.)
There's more…
Here, we applied caching on renderings globally. However, it's not recommended all the time. So, we can also apply caching options to renderings on the template or item level as well. For this, select the template standard value or content item from the Content Editor, and open the Layout Details dialog. Select any rendering; it will open Component Properties, where you will find the same caching settings.
See also
You can also create custom caching parameters such as Vary By Cookie; you can learn more about this from https://goo.gl/QANKeq. If you are eager to learn more about different types of Sitecore caching, refer to http://goo.gl/YHUwD2.
- Android Wearable Programming
- iOS開發實戰:從零基礎到App Store上架
- ASP.NET Core 2 and Vue.js
- Quarkus實踐指南:構建新一代的Kubernetes原生Java微服務
- H5頁面設計:Mugeda版(微課版)
- C++面向對象程序設計習題解答與上機指導(第三版)
- Android系統原理及開發要點詳解
- UI設計全書(全彩)
- Zabbix Performance Tuning
- Hadoop大數據分析技術
- LabVIEW數據采集
- Android高級開發實戰:UI、NDK與安全
- Java編程指南:語法基礎、面向對象、函數式編程與項目實戰
- JavaScript Mobile Application Development
- Learning Perforce SCM