EPiWiki.se  - EPiServer notes shared with others
 

Self invalidating object cache

[Edit]
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 events
Caching in EPiServer CMS
Cached user control that expires when a page is published
Disable clear cache propagated from remote events

Example 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;
}
Version author:
Mattias Lövström

EPiServer version

All