官术网_书友最值得收藏!

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:

  1. Select the Breadcrumb rendering from the Content Editor. In the field editor pane, go to the Caching section. You will find the following options:
    How to do it…
  2. 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.
  3. 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.
  4. You can apply the same settings for Title and Body, Side Menu, and other renderings as well.
  5. 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.

Note

The whole HTML cache gets cleared on each publish. So, it's wise to avoid unnecessary publishing.

HTML cache statistics

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.

HTML cache statistics

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:

HTML cache statistics

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.

When to avoid caching

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.

主站蜘蛛池模板: 乐陵市| 安溪县| 凤山县| 娄底市| 青海省| 枝江市| 昌江| 普洱| 广饶县| 乐平市| 胶南市| 长岛县| 济阳县| 云霄县| 沧州市| 苏尼特右旗| 军事| 得荣县| 保康县| 积石山| 华阴市| 罗山县| 台湾省| 昌黎县| 淄博市| 高平市| 侯马市| 岫岩| 湘潭市| 桑日县| 灵武市| 兴海县| 循化| 邮箱| 西乌珠穆沁旗| 榆社县| 琼结县| 泽库县| 九龙县| 阳朔县| 沂源县|