When we talk about a website, performance play an important role. In this article I would like to explain what are the different techniques are available for caching in Sitecore and they work.
Hopefully this will hep you in improving our site performance. The different types of cache available are
1. Prefetch Cache: For each database there is a prefetch caching. In this caching the items are pull out from database during startup. This cache contains the items that Sitecore access during and just after initialization of instance. These cache are maintained during life cycle of the application.
The Items which should be loaded as prefetch are defined in the config file specified in /app_config/prefecth folder and are the combination of the items specified in the config file for the specific database (for instance master.config) and the common.config file.
The purpose of this caching is long startup but faster load time at run time.
2. Data Cache: This is also defined for each database. the contains the items of the Sitecore class ItemInformation.
This is populated whenever they are requested. The data is pulled from the prefetch cache, if it present there; else it is pulled out of the database, put into the prefetch cache and into then the data cache.
The purpose of this cache is to minimize the amount of requests to the database. This is extremely important for performance, as requesting items from the database is rather expensive.
We can specify the maximum size of the data cache in the web.config. You can either alter the DefaultDataCacheSize setting:
Or you can set it per database in the /databases/database section:
3. Item Cache: Same as data and prefetch cache, there is an item cache for each database. This cache contains objects of the Sitecore class Sitecore.Data.Items.Item, which are the one we use in our code most of the time.
The items are built from the ItemInformation objects in the data cache. So if you request an item, which aren’t present in the item cache, it will get it from the data cache and then populate it in the item cache as an Item.
The purpose of this cache is to gain performance so the ItemInformation objects don’t need to get parsed into an Item each time they are requested.
4. HTML cache (Web Cache): The cache caches the actual HTML generated from sublayouts and renderings. There is a cache per site in your solution. You can configure your HTML cache per presentation control.
You can specify the HTML cache to vary on different parameters. For instance you can set it up so it varies by query strings; then there will be stored multiple versions of the HTML depending on which query string have been specified.
In this image you can see that I have opened the configuration of a given sublayout. I have specified that it is cachable, so it will create an HTML cache for the given page. I have also specified that the cache should vary by data, which means that the HTML should be different if the Sitecore.Context.Item is different. In this way the HTML cache will be different depending on which item you are trying to access. Further I have set up that the cache should vary by querystrings, which will cache different HTML if a querystring is different. This is great when you for instance are using paging with querystrings.
The HTML cache is really important; especially if you have renderings or sublayouts, which are rather slow. An example can be a menu which iterates over a large amount of items. When a rendering like this is HTML cached, it renders in constant time no matter how many items are to be rendered in the menu.
You can configure the allowed size of the HTML cache and whether it should be enabled or not for each of your sites. This is done in the sites section of the web.config or sitecore.config in /app_config folder. See the following line:
<site name="website" enableTracking="true" virtualFolder="/" physicalFolder="/" rootPath="/sitecore/content" startItem="/home" database="web" domain="extranet" allowDebug="true" cacheHtml="true" htmlCacheSize=”50MB” registryCacheSize=”0″ viewStateCacheSize=”0″ xslCacheSize=”25MB” filteredItemsCacheSize=”10MB” enablePreview=”true” enableWebEdit=”true” enableDebugger=”true” disableClientData=”false” cacheRenderingParameters=”true” renderingParametersCacheSize=”10MB”/>
There are few other caches in Sitecore, but they aren’t as important for performance. These are
1. XSL cache: This caches the XSLT files you have (for instance in /xsl) so they aren’t read from the disc each time you need to use it
2. Standard values cache: Caching the standard values set on your templates.
3. Paths cache: This cache binds paths (such as /sitecore/content/home) to specific IDs
4. Registry cache: This cache stores user-based registry settings
The flow of Request and Cache.
- If the item which is requested has presentations which are cached, the HTML from the HTML cache is returned and the request is ended.
- If there isn’t any HTML cached all items needed by the presentation is requested from the item cache.
- If the items aren’t in the item cache it is retrieved from the data cache and parsed into an Sitecore.Data.Items.Item and stored in the item cache.
- If the item isn’t present in the data cache, it is retrieved from the prefetch cache and the ItemInformation is stored in the data cache.
- If the item isn’t in the prefetch cache it is retrieved from the database and stored in the prefetch cache as PrefetchData.
Cache Clearing work differently depending on environment you have. The different caches are cleared like this:
HTML cache: The HTML is always cleared on publish. So whenever you publish even a single item the complete cache is cleared. This is due to the fact, that parts of the HTML can be built from multiple items, so the cache has no way of knowing if the item being published has influence on the presentation.
Item cache: The item cache is updated incrementally in an atomized way, so if an item is updated during a publish it is updated in the item cache as well. If you publish an item which might influence other items, these items are cleared as well. For example if you publish standard values or a template, that clears more than a single item.
Further the cache is completely cleared by default if you use the staging module.
Data cache: Also the data cache is incrementally updated, when changes take effect after a publish. The cache is completely cleared by default if you use the staging module. It is rebuild incrementally when the items are requested again.
Prefetch cache: Is cleared in the same way as the data cache, but the items specified in the config files are prefetched from the database.
The important part to notice here is that publishing can make quite a big impact on performance; especially if you are running in a staged environment; so you should consider your publishing strategy carefully. You often get the best result by using scheduled publish.