Sitecore caching

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

They are:

Description of all admin pages could be found by the following link
http://dinbror.dk/blog/sitecore-admin-pages-cheat-sheet

Different Database Cache

Prefetch 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 Cache

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 >

Item Cache

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.

html-cache-specification-example-global
Global HTML cache specification example
html-cache-specification-example-pagelevel
Dynamic HTML cache specification example
sitecore-html-caching-varybyparameters
VaryBy cache parameters

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.

Media Cache

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.

User Cache

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.

Proxy Cache

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.

Other caches

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:

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.

how-sitecore-cache-works
How Sitecore cache works
  1. 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.
  2. If there isn’t any HTML cached all items needed by the presentation is requested from the item cache.
  3. 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.
  4. 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.
  5. 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.

HTML cache

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.

Item 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

Data cache is updated incrementally when changes take effect after a publish. It is rebuild incrementally when the items are requested again.

Prefetch cache

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.

Advertisements

One thought on “Sitecore caching

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