tags:

views:

27

answers:

1

Hi!

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! :)

+1  A: 

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;
Ted Nyberg