Skip to content
October 30, 2011 / kiranpatils

Basics of HTML Caching

Challenge:

HTML Caching plays a vital role in improving performance of Sitecore front-end websites, there are the good amount of information (posts) available. Through this post, I will be trying to put them under one post, along with my learning!

Solution:

Before implementing any new thing, I ask three questions to my self What, Why, How (WWH) . And if you know answers of all these three questions, you can easily learn and implement anything! So, let’s start answering them one by one:

What is HTML Caching?

HTML Caching is first layer of cache which gets checked whenever user requests any page (If you are new to Sitecore caching, I would strongly recommend you to read my earlier post on Sitecore Caching). It caches the data generated and packaged from bottom layers at top layers. So, on each request Sitecore don’t have to do all process again which is already done!

Why HTML Caching?

As we know Sitecore is a CMS and it stores everything in a raw format in database and on request, it converts that raw data in Item and then finally HTML format — This is what browser understands!

Usually, there are some portion in site which are not regularly updated or they are shared across all the pages e.g. Header, Footer, Menu etc. So, Using HTML Caching we can cache that output for better performance! (Which leads to better user experience and happy client(s) :) )

How to apply HTML Caching?

You can apply HTML Caching (before applying HTML Caching make sure cacheHtml=”true” under sites section) from Global Renderings : (Image Source : http://sitecoreblog.alexshyba.com/2011/05/how-to-verify-html-cache-is-working.html )

SNAGHTML1cd434f

OR

at Page level rendering as well as shown below : (Image Source :http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2009/07/CachingOverview.aspx)

Cache setting

For more information, please refer Presentation Component Reference : http://sdn.sitecore.net/Reference/Sitecore%206/Presentation%20Component%20Reference.aspx and http://sdn.sitecore.net/upload/sitecore6/sc62keywords/cache_configuration_reference_a4.pdf

If you would like to understand about each check box e.g. Vary by Data, Vary by Device etc. then refer following screen-shot [Source : NLC’s whitepaper):

How to disable HTML Caching for CM environment?

In Sitecore world few people create two URLs for same site one is for CM environment (Points to Master DB) — Where Editors work and one is for CD environment (Points to Web DB) – Where front end users access the live site!

Now, If you would like to disable HTML Caching for CM environment and would like it to be available for CD environment then you can do it by changing cacheHtml attribute’s value to false under Sites/Site section for CM Site.

<site name="CMwebsite" ... cacheHtml="false" ... />
How to check applied HTML Caching is working or not?

To do this task, Alex Shyba has already written nice article on it : http://sitecoreblog.alexshyba.com/2011/05/how-to-verify-html-cache-is-working.html

Basically, I prefer to use /sitecore/admin/cache.aspx/stats.aspx OR if you want to see what lies in each HTML Cache entry (More useful for expert level!) use http://trac.sitecore.net/CachingManager

What is the HTML Cache Size Limit? Is it possible to increase it?

By default HTML Cache size comes from following attributes from Web.config:

1. Caching.DefaultHtmlCacheSize : Determines the default size of the html cache of a site specify the value in bytes or append the value with KB, MB or GB

<setting name="Caching.DefaultHtmlCacheSize" value="10MB" />

2. htmlCacheSize : under Sites/Site section. If you have provided value here it will override the value provide in #1.

<site name="website" ... cacheHtml="true" htmlCacheSize="10MB" />

3. section next to the section.

<cacheSizes>
<sites>
<website>
<html>10MB</html>
<registry>1MB</registry>
<viewState>1MB</viewState>
<xsl>5MB</xsl>
</website>
</sites>
</cacheSizes>

Yes, you can increase (or in some cases decrease) HTML Cache size value as per your need!

When HTML Cache will get cleared?

On publish end (if you are using Sitecore Version >= 6.3 then on publish:end:remote) it will clear HTML Cache for configured sites. As shown below from event section of your web.config file:

 <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
 <sites hint="list">
 <site>website</site>
 </sites>
 </handler>
 

Just a note : If you are running Multi-Site from your Single Sitecore instance then on publish of one site, it will clear HTML Cache for other sites as well, even-though it is not required. To overcome this issue you have to hand-craft your own handler! We’ve already done this. So, if you need any help give a shout!

How it works?
  • Theoretically : On each request, it checks whether requested data is available in HTML layer cache or not. If found it will return the response from that layer else it will go in other layers generate the data, using renderings logic convert it in to HTML and if HTML Caching enabled then generated output will be cached in HTML Cache and served to an end-user. So, on next request it will server data from HTML Cache, the first layer of cache!
  • Technically: 1. While generating a page ASP.NET Calls the Page.Render() method of Sitecore.Web.UI.WebControl
    2. Caching logic
    If (IsKeyExistinHTMLCache)
    {
    Render() method writes the cached data to the output stream;
    }
    else
    {
    Calls the DoRender() method,
    If (Control is Cacheable)
    {
    the Sitecore.Web.UI.WebControl.Render() method adds the output of the control to the output cache.
    }
    } 3. Key generation logic : for generating each cache key when you mark control as cachable, Sitecore uses the result of calling the GetCachingID() method plus a string that identifies the context language as the key for the cache entry. If you select additional VaryBy parameters, Sitecore adds additional strings to the key. Would you like to see Cache Keys and Cache entries? http://trac.sitecore.net/CachingManager will help you to do it!

Source : http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/09/How-the-Sitecore-ASPNET-CMS-Caches-Output.aspx

We are using WebControls for our renderings. Do I have to do anything extra to make them HTML Cache supportable?

Nothing special, Just make sure your control does following two required things:

1. Inherits from Sitecore.Web.UI.WebControl abstract base class

2. implement the DoRender() method

3. implement the GetCachingID() method

For more information please refer : http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/09/How-the-Sitecore-ASPNET-CMS-Caches-Output.aspx

HTML Caching will work with postback controls?

No, It won’t work. For more information please refer : http://stackoverflow.com/questions/3628185/sitecore-turning-on-html-caching-blocks-postback-behavior

Happy HTML Caching! :)

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: