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?
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).
Hi Magnus
If its like you mentioed what i have to do now? Because my page has no child pages.
Suggest me
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.
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) });
Even in Episerver 7 almost four years after the last comment from Magnus Rahl still is valid and working as expected.
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);
HI
I am using Cache objects in my page.