November Happy Hour will be moved to Thursday December 5th.

Null in Cache objects in EPiServer with page dependencies

Vote:
 

HI

I am using Cache objects in my page.

          var dependency = DataFactoryCache.CreateDependency(CurrentPage.PageLink);
          CacheManager.RuntimeCacheInsert("myCacheKey",objectToCache,dependency);

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

Any idea?
 

 

#53389
Sep 07, 2011 8:25
Vote:
 

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?

#53390
Sep 07, 2011 9:11
Vote:
 

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).

#53391
Sep 07, 2011 9:18
Vote:
 

Hi Magnus

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

Suggest me

 

#53404
Sep 07, 2011 11:15
Vote:
 

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.

#53405
Sep 07, 2011 11:41
Vote:
 

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) });

#53406
Sep 07, 2011 11:45
Vote:
 

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

#123311
Edited, Jul 01, 2015 12:24
Vote:
 

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>()
{
DataFactoryCache.PageCommonCacheKey(objectReference),
DataFactoryCache.PageMasterLanguageCacheKey(objectReference)
},
new TimeSpan(0, 10, 0),
CacheTimeoutType.Absolute
);

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

#136925
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.