I managed to solve this. Using the method found here.
I created a simple implementation like (not threadsafe):
private static ISynchronizedObjectInstanceCache _cacheInstance;
public static ISynchronizedObjectInstanceCache CacheImplementation
{
get
{
return (_cacheInstance ?? (_cacheInstance = ServiceLocator.Current.GetInstance<ISynchronizedObjectInstanceCache>()));
}
set
{
_cacheInstance = value;
}
}
public IList<MyObject> GetCachedObjects()
{
if (_cachedObjects == null)
{
object value = CacheImplementation.Get("MyObjectsCacheKey");
if (value == null)
{
// Lookup objects (the slow function that triggered the need for caching)
_cachedObjects = LookupMyObjects();
string currentLanguage = ContentLanguage.PreferredCulture.Name;
// Create dependencies and add to cache
List<string> cacheDependencyKeys = new List<string>();
// Add dependency to the start page
cacheDependencyKeys.Add(DataFactoryCache.PageLanguageCacheKey(ContentReference.StartPage, currentLanguage));
// Add dependency to the pointed out startpoint of the lookup of "MyObjects" (if not null)
// This will force a cache invalidation when the page itself or it's children are changed.
if (CurrentPage.MyObjectsStartPoint != null)
{
cacheDependencyKeys.Add(DataFactoryCache.PageLanguageCacheKey(CurrentPage.MyObjectsStartPoint, currentLanguage));
cacheDependencyKeys.Add(DataFactoryCache.ChildrenCacheKey(CurrentPage.MyObjectsStartPoint));
}
// Do insert into cache
CacheImplementation.Insert("MyObjectsCacheKey", _cachedObjects, new CacheEvictionPolicy(cacheDependencyKeys.ToArray()));
} else
{
_cachedObjects = value as IList<MyObject>;
}
}
return _cachedObjects;
} private IList<MyObject> _cachedObjects = null;
Edit: Changed to ISynchronizedObjectInstanceCache instead of IObjectInstanceCache. EPiServer's "CacheManager"-class uses this implementation, and is supposed to work with cache invalidation between servers.