This gives a ton of options and in most cases this is sufficient, but what if the output of your rendering depends on inner logic embedded within the control? Sometimes you could solve this with VaryByParm and by passing different parameters to the rendering, but what if that’s not the case?
This is exactly the use case I have been presented during another onsite visit with a customer.
Here is a very simple way of extending the cache variation logic for a Sublayout.
If you are Sitecore blackbelt, consider moving on to the “Solution” part below.
Consider the following example.
We have a new list control which we want to personalize by “audience”. When user is authenticated, we imagine a process that “attaches” the current user session with a particular “audience type” and saves this in a cookie. In order to simulate this, I’ve created a control called “AudienceSwitcher” which sets the current “Audience Type” explicitly for this sake of this example:
Whenever “Audience Type” changes, the following method is called:
AudienceManager is a custom class that incorporates all audience related actions, very simple:
This has an effect on the NewsList control, which renders only the news items that are targeted to a specific audience type:
All news items are stored under /home/news and tagged to an audience via a multilist:
The “Audience Types” referenced from the multilist above are stored in a separate area in the content tree:
After verifying that our NewsList control is working correctly and displaying different news for different audiences:
for travel agent
Let’s try and cache it this rendering by checking Cacheable and VaryByData.
If you don’t run live mode, you would need to publish after this.
The expected result of this configuration will not be satisfactory. The same output of the NewsList control will be cached once and served up from memory every time it is rendered, no matter what the current “Audience Type” is.
Here is the solution.
1. Subclass the default Sublayout implementation:
Very basic what we do here. We override the default implementation of the GetCacheKey method which is evaluated by Sitecore’s output caching mechanism every time a control needs to get cached. The key returned by this method needs to uniquely identify the control’s output in the cache collection.
Line 10 is the most important, this is where we call our friend AudienceManager and reading the current audience key (basically a value from cookie).
2. It’s important to note that this is done only if “VaryByParm” is checked on the control definition item in Sitecore, so we need to make sure it is checked:
The last thing you need to do is to wire in the custom AudienceSublayout by overriding the “SublayoutRenderingType” component:
3. Compile the following class:
The only think we change here is line 5. Instead of the default Sublayout we instantiate our custom AudienceSublayout.
4. Define custom SublayoutRenderingType in web.config, line 3:
That’s all you need to do. Now the output of the NewsList control will vary depending on the current “Audience Type”.
Here is where you can download all the sources for this post:
Obviously, this is only one way of doing it. If you have other ideas, please share in the comments below.
Have a great weekend!