It is possible to use DataFactory.VersionKey that is updated each time the database is updated as a cache dependency, this means that the cached object will be removed each time the database is changed. (for larger sites this will update the database to often, but it will be enough for smaller sited)
References
EPiServer CMS Remote eventsCaching in EPiServer CMSCached user control that expires when a page is publishedDisable clear cache propagated from remote eventsExample of a self invalidating cache in EPiServer
public static string MyCacheKey = "MyCacheKey";
public PageDataCollection GetPages()
{
var ret = CacheManager.Get(MyCacheKey) as PageDataCollection;
if (ret != null)
{
return ret;
}
ret = LongTimeToLoadMethod();
Cache.Insert(MyCacheKey,
ret ?? new PageDataCollection(),
new CacheDependency(null,
new string[]{DataFactoryCache.VersionKey}));
return ret;
}