Try our conversational search powered by Generative AI!

Null in Cache objects in EPiServer with page dependencies



I am using Cache objects in my page.

          var dependency = DataFactoryCache.CreateDependency(CurrentPage.PageLink);

But every time i am getting NULL in
var cachedObject = CacheManager.RuntimeCacheGet("myCacheKey")

Any idea?


Sep 07, 2011 8:25

The ASP.Net cache works so that the cacheKey you set a dependency to must exist in the cache. If not your object will be evicted from cache immediately. So in your case could it be so that the dependent object is not present in cache?

Sep 07, 2011 9:11

I think i know why:

The depencency you get from data factory cache consists of three keys: One for the page you passed in itself, one "master key" for all pages and one key for the page's children list.

A key/item is evicted from the cache as soon as any of it's dependencies are evicted. This is also true at the instant of inserting - if one key is missing the object won't be inserted.

My guess is that at the time when you do this insert the page's child list has not yet been cached (the other two should absolutely be cached).

Sep 07, 2011 9:18

Hi Magnus

If its like you mentioed what i have to do now? Because my page has no child pages.

Suggest me


Sep 07, 2011 11:15

Well, you can probably verify it by adding a GetChildren(CurrentPage.PageLink) above the lines in your example. If it works then you've found the culprit.

I assume you are seing this while debugging, and you just started the site. The real world your site will at some point probably get the children of the page (for a menu listing or whatever) and then it will be cached. The fist time your method is hit after that it will also be able to cache its object and it will sit in the cache until the page or its children are changed.

Sep 07, 2011 11:41

Or if your cache object isn't logically coupled to the pages child pages you could of course construct a cache dependency without a dependency to the GetChildren key, like so:

new CacheDependency(null, new string[] { DataFactoryCache.PageCommonCacheKey(CurrentPage.PageLink), DataFactoryCache.PageMasterLanguageCacheKey(CurrentPage.PageLink) });

Sep 07, 2011 11:45

Even in Episerver 7 almost four years after the last comment from Magnus Rahl still is valid and working as expected.

Edited, Jul 01, 2015 12:24

Just an update on this since 7.5 has introduced the CacheEvictionPolicy object.

A working example for caching with a dependency to a IContent with an absolute cache timeout of 10 min:

var dependecy = new CacheEvictionPolicy(
new List<string>()
new TimeSpan(0, 10, 0),

CacheManager.Insert("yourcachekey", object, dependecy);

Sep 18, 2015 11:56
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.