Cache objects in EPiServer with page dependencies

This article was migrated from an older iteration of our website, and it could deviate in design and functionality.

EPiServer comes with its own set of cache management classes to help you cache objects which depend on specific EPiServer pages, and also ensure cache updates are propagated among multiple web servers.

Estimated read time : 5 minutes

Jump to

Caching in EPiServer compared to standard ASP.NET caching

Experienced ASP.NET developers will have used the Cache property of the HttpRuntime class to cache objects in memory like this:


This will add an object to the current runtime’s cache. The dependency parameter could be a dependency to a database, the file system, or some other object which should trigger a cache update if changed – such as an EPiServer page.

This works fine, unless you’re in a multiple-servers setup, for example a load-balanced configuration of an EPiServer website. The code above would add the objectToCache object to the cache of the server on which the code is executed. This could have serious performance impacts, or worse.

EPiServer-specific cache classes

There are two classes in EPiServer you will want to be familiar with: CacheManager and DataFactoryCache.

The CacheManager class

The CacheManager class is used to add and get cached objects. It can be used for adding standard memory cache objects or adding objects to the runtime cache.

One major difference when using EPiServer’s own cache classes is that cache changes are propagated to load-balanced web servers as well. 

The DataFactoryCache class

The DataFactoryCache class is used by the DataFactory to clear cache objects in response to events such as when a page is published. It can also be used by a developer to explicitly clear the cache after modifying EPiServer pages without using the EPiServer API (which is a major no-no in my book, but still).

Another reason for using the DataFactoryCache class is to create dependency objects. Dependency objects are objects which will trigger cache updates when they change. Quite often in EPiServer we cache objects and want these cached objects to be updated in response to EPiServer pages being changed.

Cache an object until an EPiServer page changes

To create a dependency to an EPiServer page we use the CreateDependency method of the DataFactoryCache class and passing in a PageReference object specifying which page our cache object should depend on:

// Create a dependency which could be used to cache an object until
// the current page changes, such as when an editor publishes a new version
var dependency = DataFactoryCache.CreateDependency(CurrentPage.PageLink);

Now we can use this dependency to cache an object and keep it in memory until the specified page changes:


Now, if we would re-publish the page the cached object would be cleared.

Retrieve a cached object

Retrieving cached objects is also done using the CacheManager class. If you’ve added an object to the cache using the RuntimeCacheInsert method you’ll use the RuntimeCacheGet method to retrieve it:

// Retrieve a cached PageDataCollection object
var cachedObject = CacheManager.RuntimeCacheGet("myCacheKey") as PageDataCollection;
    // Update cache

More on EPiServer caching

Don't miss Joel Abrahamsson's posts on EPiServer caching, links are in the sidebar!