Showing posts with label caching. Show all posts
Showing posts with label caching. Show all posts

Monday, 23 March 2015

Creating your own context objects

Context objects


Sitecore provides a number of contexts to allow you to enable and disable certain framework features on a case by case basis. For example if you want to run a piece of code without any security concerns you can use the SecurityDisabler.
using (new SecurityDisabler())
{
    // your code here
}
Any code inside the SecurityDisabler block will ignore all security concerns, so it can create items, edit items, delete items etc without the logged in user having the appropriate permissions. Another common context to use is the BulkUpdateContext. If you are doing a lot of updates to Sitecore items as part of a maintenance routine then putting your code inside a BulkUpdateContext stops Sitecore from recording things in the history engine which will increase the performance of your code.  Similarly the EventDisabler prevents events from being raised.

It's a handy technique that you might want to leverage yourself. In one solution we had caching built at a deep layer, but for certain admin functions we didn't want to use the cache, we always needed the actual data. The solution was to use our own context that could disable caching.

Tuesday, 3 February 2015

Using .net caching with Sitecore

Introduction


Sitecore has an extensive caching framework that allows it to cache everything from items in its database all the way to the html output of its renderings.  When using Sitecore's cache framework it employs a number of techniques to ensure that the cached data remains valid, and if you use .net's own caching framework this can sometimes conflict with Sitecore.  For example, let's say you have a menu control that appears on every page so you use .net's output caching to cache the html.  If a content editor changes something in Sitecore that affects your menu such as creating new elements, renaming titles etc, then your site is going to continue serving up the cached version which is no longer valid.  If you use Sitecore's own caching framework by setting cache parameters on the rendering then Sitecore will clear these cached items when a publish happens, ensuring that the cached html is rebuilt using the new data.  Furthermore, Sitecore will disable caching when you are in preview or edit mode so you know your data is always fresh.

As well as html caching, you might want to cache data structures you have built from Sitecore items.  Let's say you have a list of countries and in each country is a list of cities and you use this data often, so would like to cache it.  If you use .net caching and the underlying data in Sitecore changes, your cached data is now invalid.  You could set an expiry on the cache, but that's far from ideal as you don't want to tell your content editors that they have to wait before they see their changes go live.
In this article I am going to show you how to hook into the publishing mechanism such that your cached items are removed when a publish occurs.