In this post I would like to describe different levels of Sitecore caches. Cache plays a vital role in website performance. So, it is really important to understand how Sitecore cache works, from which parts consists and how it could be configurated. If we master these skills it would be easy to do performance turning using cache settings.
There are some useful tools which can show us the structure and content of Sitecore cache
this tool displays details about the caching settings configured
this tool describes how actual cache is managed by Sitecore
Description of all admin pages could be found by the following link
Different Database Cache
There is a prefetch cache for each database. Prefetch cache contains items that Sitecore accesses during and immediately after initialization and items with children that Sitecore often accesses as a group. Sitecore maintains those caches over the life of the application.
Each database prefetch cache entry represents an item in a database. Database prefetch cache entries include all field values for all versions of that item, and information about the parent and children of the item.
The items which should be loaded at start up are specified in the config files in /app_config/prefecth folder. The loaded items are a combination of the items specified in the config file for the specific database (for instance master.config) and the common.config file.
The obvious purpose of this cache is of course to trade longer startup items for faster load items at runtime.
Data caches are dependent on database prefetch caches, which operate at a lower level. Like database prefetch caches, each entry in a database data cache represents a single item in a database, including parent/child relationships and field values for all versions in all languages of that item.
The cache 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 then into 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 are rather expensive.
The Caching.DefaultDataCacheSize setting in the web.config file specifies the default size for database data caches.
<setting name = "Caching.DefaultDataCacheSize" value="10MB" />
The size for data cache could also be specified per database in the /databases/database section:
<cacheSizes hint = "setting"> < data > 20MB </data> < items > 10MB </items> < paths > 500KB </paths> < standardValues > 500KB </standardValues> </ cacheSizes >
As with the data and prefetch cache, there is an item cache for each database. Item caches store items (they are of Sitecore.Data.Items.Item type), which are the one developer use in the code most of the time. So if developer requests an item, which isn’t present in the item cache, he will get it from the data cache and then populate it in the item cache as an Item.
Each entry in a database item cache represents a single version of an item in a single language. Sitecore does not pre-populate database item caches.
The Caching.DefaultltemCacheSize setting in the web.config file specifies the default size for database item caches.
HTML Cache / Web cache
The HTML cache – also referred to as the web cache – caches the actual HTML generated from the sublayouts and renderings. HTML cache is associated with each managed Web site. It contains the output generated by individual rendering under different conditions.
Sitecore provides caching options which allow the rendered data to be retrieved from cache if the data source, device, authentication status, user, rendering parameters and/or query string parameters are the same as the previous request.
Sitecore allows developers to define output cache criteria in three places:
- In the Caching section of the sublayout and rendering definition item. (Global)
- In the properties of the presentation component when you statically bind it to a layout or sublayout. (Static Controls)
- In the Caching section of the Control Properties dialog when you bind a presentation component to a placeholder in layout details. (Dynamic)
HTML cache is disabled in the preview, webedit and debug modes.
Filtered Item Cache
The filtered items cache associated with each managed Web site contains information about versions of items relevant to different users.
The filteredItemsCacheSize attribute of each /configuration/Sitecore/sites/siteelement in the web.config file specifies the size of the filtered items cache for that managed Web site.
The Caching.DefaultFilteredItemsCacheSize setting in the web.config file specifies the default size of the size filtered items caches.
Sitecore stores all media files to physical file system. All other cache are stored in RAM actually.
Please note! When publishing is done, Sitecore does not clear Media Cache like it does for other caches. Sitecore clears these media cache periodically.
The client data store cache stores information about each authenticated user, such as the username or other user properties.
The Caching.DefaultClientDataCacheSize setting in the web.config file specifies the size of the client data store cache.
The disableClientData attribute of each /configuration/Sitecore/sites/site element in the web.config file enables or disables client data caching for that managed Web site.
Sitecore has a proxy item feature that allows items in one area of the content tree to appear in another.
These proxy items behave in the same way as normal items but have unique IDs to distinguish them from the original items.
The proxy cache keeps track of these IDs and how they map back to the original items.
There are quite a few other caches in Sitecore, but I have found out that they aren’t as important for performance as the ones already mentioned. There are the following other mentionable caches:
- 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.
- Standard values cache: Caching the standard values set on your templates.
- Paths cache: This cache binds paths (such as /sitecore/content/home) to specific IDs. Read more about it here: http://sitecorekh.blogspot.com/2007/08/understanding-sitecores-path-cache.html
- Registry cache: This cache stores user-based registry settings. Read more about it here: http://sitecoresupport.blogspot.com/2009/07/registry-cache-for-website.html
Overview of requests and caches
So what and when are things actually cached? Well look at this graph, which shows how a request is handled.
- If the item which is requested has presentations which are cached, the output (HTML markup) 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.
Different Website Cache
If you don’t specify cache at a level then it gets its values from default website cache in the web.config. We can customize all these cases site-wise.
<cacheSizes> <sites> <website> <html>10MB</html> <registry>0</registry> <viewState>0</viewState> <xsl>5MB</xsl> </website> </sites> </cacheSizes>
How cache clearing works?
Sitecores caches are cleared differently depending on your environment. A clearing of the cache can be brutal to the performance of your site until it is rebuilt.
On publishing of any item, HTML cache is cleared. If we use multisite module, we can rewrite HTML cache module to clear HTML cache for item’s related site. HTML cache for each page is built from multiple items, so publishing a single item, we cannot judge at how many items it would affect. That’s why we have to clear full site HTML cache.
Whenever any item is published, its Item Cache is also updated. If you publish an item which is linked to other items, then these items are cleared as well.
If you publish standard values or a template, it will clear all items based on that template.
If you delete/recycle/restore any item, its parent item’s cache also updated, similarly while doing sorting, their siblings cache might get updated.
Data cache is updated incrementally when changes take effect after a publish. It is rebuild incrementally when the items are requested again.
By default, items/templates specified in the config file are cached in Prefetch Cache when application initiated. Prefetch cache are updated same way of Data Cache.
Note: An ASP.NET application server restarts effectively removes all entries from all caches, except media cache. If we are using ASP.NET caching based on some items’ values, we must clear ASP.NET cache too on publishing.
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. My experiences is that you often get the best result by using scheduled publish.