Tutorial :DataFactoryCache.CreateDependency in EPiServer returns CacheDependency with HasChanged==true



Question:

I'm using the following code to cache an object using the CacheManager class in EPiServer:

CacheManager.RuntimeCacheAdd(                  cacheKey,                   myPageCollection,                  DataFactoryCache.CreateDependency(new PageReference(15)));  

For some reason the CacheDependency object returned has HasChanged==true, even though the LastModified value doesn't change (and is indeed before DateTime.UtcNow).

In other words, my cached object will never stick since the dependency instantly clears the cache (because of HasChanged==true).

Any suggestions are greatly appreciated! :)


Solution:1

This isn't really a complete answer to "why", but here's what I can see:

When you create a CacheDependency for a specific page, that CacheDependency will have HasChanged==true until children for the page have been retrieved (and are thus cached).

So, in order to ensure my CacheDependency is properly initialized I have to go from this:

var cacheDependency = DataFactoryCache.CreateDependency(new PageReference(15)); // cacheDependency.HasChanged==true;               

to this:

var children = DataFactory.Instance.GetChildren(new PageReference(15));    var cacheDependency = DataFactoryCache.CreateDependency(new PageReference(15)); // cacheDependency.HasChanged==false;  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »