Extending Sitecore Html/Sublayout caching library
What it does
- Add an cache timeout per sublayout
- Show cache debug information per sublayout
- Globally disable all subalyout caching (e.g local environments)
- Turn sublayout caching on/off dynamically based on your own code.
Get it at
https://marketplace.sitecore.net/Modules/SublayoutCachingExtender.aspx
The basics
Every web developer is aware that caching can dramatically improve performance. With Sitecore we have so many levels of caching (here is a great read just about this) - today we will concentrate on html/sublayout caching.
The easiest way to enable a sublayout for caching is on the sublayout item itself in Sitecore. Simply check the 'Cacheable' checkbox and Sitecore will serve a cached version of the Html next time the control is requested. The 'Vary by' options alter the cache key so you are able to cache different versions of the control. Read more about this here.
A good rule of thumb is to not cache any controls which vary user to user or postback to the server.
Limitations
The main limitation is that once a control is in cache, it remains cached until a publish clears the cache (if you have this configured), you manually clear the cache or the web app is restarted.
What if we want to expire the cache every 2 minutes for example? (A control displaying price information is a good example of when you'd like to do this). This is not possible out of the box.
Another limitation is that we cannot easily see which controls are cached when or how long they have been cached for.
Usage
Add the assembly into your solution and the patch file into App_Config/Include
You now get these neat things:
1. Debug
In performance.config set showdebug="true" to render debug information against each sublayout.
2. Per control expiry
Within performance.config
Note: The sublayout must be set as 'cacheable' in Sitecore!
Here's an example:
<sublayoutcaching> <controls> <control path="/Sublayouts/MyControl.ascx" hours="0" minutes="5" seconds="0"></control>
3. Globally disable sublayout caching
Simply set disablesublayoutcaching="true" and you will always see the uncached version of the sublayout even if 'Cachebale' is checked.
4. Disable caching dynamically
You may have a control which displays pricing information. Logged in users might see a different price compared to those users who are not logged into the website.
Therefore we want to dynmically cache this control - we want the cached version to display for non-logged in users but we want a personalised version of the control (uncached) for logged in users.
To do this Add a disablecacherule, some examples are included for you.
Here is an example of a rule which will show an uncached version of sublayouts for logged in users:
public class UserIsLoggedIn : IGlobalSublayoutCacheRule
{
public bool IsCacheable()
{
return !Sitecore.Context.IsLoggedIn;
}
}
And update performance.config:
<disablecacherules>
<add type="CacheExtender.Implementation.UserIsLoggedIn, Sitecore.CacheExtender"></add>
Happy caching!