First an exlanation on what happens: The ASP.NET cache works so that if an item is added with a dependency and if the item corresponing to the dependency does not exist in the cache then the item is evicted immediately.
Looking at the source code for CreateDependency method reveals that it is implemented like:
public static CacheDependency CreateDependency(ContentReference contentLink)
{
return new CacheDependency(null, new string[3] { PageCommonCacheKey(contentLink), PageMasterLanguageCacheKey(contentLink), ChildrenCacheKey(contentLink) });
}
meaning that it sets up a dependency to the content item but also the children listing for the content. That explains why GetChildren solves the issue since that call will add the children listing to cache.
However I cant see no reason for the dependency to the children listing (I have reported a bug that we should remove that dependency) so what you can do is to instead of using method CreateDependency use DataFactoryCachce.PageCommonCacheKey(contentLink) to get the dependency key.
Thanks I have tested using page and language only and it works perfect. I will add as custom extension for now until patch or update.
_tweets = UserTimeline.GetTimeLine(ref _followers);
var cacheMinutes = 20;
var dependency = new CacheDependency(null, new string[2] { DataFactoryCache.PageCommonCacheKey(CurrentPage.ContentLink), DataFactoryCache.PageMasterLanguageCacheKey(CurrentPage.ContentLink) });
CacheManager.RuntimeCacheInsert(cacheKey, _tweets, dependency, DateTime.Now.AddMinutes(cacheMinutes), Cache.NoSlidingExpiration, CacheItemPriority.Normal);
I am having the same issue as posted here
http://stackoverflow.com/questions/3300232/datafactorycache-createdependency-in-episerver-returns-cachedependency-with-hasc
Using the GetChildren resolves the issue but without it the page has to be loaded twice before the object is cached.
If the dependency is set to null the cache works fine.
Example not being cached when getting tweets from twitter
_tweets = UserTimeline.GetTimeLine(ref _followers);
var dependency = DataFactoryCache.CreateDependency(CurrentPage.ContentLink);
int cacheMinutes = 20;
CacheManager.RuntimeCacheInsert(cacheKey, _tweets, dependency, DateTime.Now.AddMinutes(cacheMinutes), Cache.NoSlidingExpiration, CacheItemPriority.Normal);
--------------------------
If I add GetChildren it works fine.
_tweets = UserTimeline.GetTimeLine(ref _followers);
var children = DataFactory.Instance.GetChildren(CurrentPage.ContentLink.ToPageReference());
var dependency = DataFactoryCache.CreateDependency(CurrentPage.ContentLink);
int cacheMinutes = 20;
CacheManager.RuntimeCacheInsert(cacheKey, _tweets, dependency, DateTime.Now.AddMinutes(cacheMinutes), Cache.NoSlidingExpiration, CacheItemPriority.Normal);