DataFactoryCache.CreateDependency in EPiServer returns CacheDependency with HasChanged==true
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! :)
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;
精彩评论