🔧 Maintenance Alert: World will be on Read-Only Mode on February 18th, 10:00 PM – 11:00 PM EST / 7:00 PM – 8:00 PM PST / 4:00 AM – 5:00 AM CET (Feb 19). Browsing available, but log-ins and submissions will be disabled.

Daniel Ovaska
May 13, 2016
  6539
(5 votes)

Making a cache dependency on pagetype or ancestor

Based on a forum post question I wanted to take the new master key concept for caching out for a test drive.

Let’s say you want to cache stuff but directly a certain page type is updated anywhere you want to clear that cache. Might be a news listing or similar. Another common request is that you want to clear cache if anything is change below a certain root page. How do we make this using master keys?

Talking in code I want to do this (but with a CacheEvictionPolicy that works on both content types and a dependant root page):

var cacheKey = "KeyForItem";
var cachedItem = DateTime.Now.ToLongTimeString();
var cache = ServiceLocator.Current.GetInstance<ISynchronizedObjectInstanceCache>();
cache.Insert(cacheKey, cachedItem, cacheEviction);

To solve this I created two classes

1. A cache "manager" class that is responsible for creating some sweet cache invalidation policies and also to help invalidate cache. 2. An initialize module that subscribes to content events. This will basically just pass along any events to the cache manager and let that class determine if something needs to be invalidated.
public interface ICacheManager
{
   CacheEvictionPolicy GetCacheEvictionPolicy(TimeSpan duration, IEnumerable<Type> dependentTypes );
   CacheEvictionPolicy GetCacheEvictionPolicy(TimeSpan duration, IEnumerable<Type> dependentTypes, IEnumerable<ContentReference> roots);
   void OnContentChange(object sender, EPiServer.ContentEventArgs e);
}
//Class responsible for creating cache eviction policies and invalidate cache depending on content events...
public class CacheManager:ICacheManager
{
    private readonly ISynchronizedObjectInstanceCache _cache;
    private readonly IContentLoader _contentLoader;
    private readonly IContentTypeRepository _contentTypeRepository;
    public CacheManager(IContentTypeRepository contentTypeRepository,IContentLoader contentLoader, ISynchronizedObjectInstanceCache cache)
    {
            _contentTypeRepository = contentTypeRepository;
            _contentLoader = contentLoader;
            _cache = cache;
    }
    //Depending on page types...
    public CacheEvictionPolicy GetCacheEvictionPolicy(TimeSpan duration, IEnumerable<Type> dependentTypes )
    {
         return new CacheEvictionPolicy(null,null,dependentTypes.Select(t=> GetMasterKey(t)));
    }
    //Depending on ancestor node in content tree...
    public CacheEvictionPolicy GetCacheEvictionPolicy(TimeSpan duration, IEnumerable<Type> dependentTypes, IEnumerable<ContentReference> roots)
    {
        IEnumerable<string> dependentTypesKeys = new List<string>();
        if (dependentTypes != null)
        {
            dependentTypesKeys = dependentTypes.Select(t => GetMasterKey(t));
        }
        IEnumerable<string> ancestorKeys = new List<string>();
        if (ancestorKeys != null)
        {
             ancestorKeys = roots.Select(p => GetMasterKeyForAncestor(p));
        }
            return new CacheEvictionPolicy(null, null, dependentTypesKeys.Union(ancestorKeys));
     }

     private string GetMasterKeyForAncestor(ContentReference parent)
     {
        return $"Descendants:{parent.ID}";
     }
     private string GetMasterKey(Type type)
     {
           
            var contentType = _contentTypeRepository.Load(type);
            if (contentType != null)
            {
                return GenerateMasterKey(contentType);
            }
            return null;
     }

     private string GetMasterKey(IContent content)
     {
        var contentType = _contentTypeRepository.Load(content.ContentTypeID);
        return GenerateMasterKey(contentType);
     }

     private string GenerateMasterKey(ContentType type)
     {
        return $"ContentDependency:{type.GUID}";
     }
     //Invalidate cache if editor has changed a matching page...
     //Remember that a page can have children that is affected as well so need to take care of those as well
     public void OnContentChange(object sender, EPiServer.ContentEventArgs e)
     {
            var masterkey = GetMasterKey(e.Content);
            _cache.RemoveLocal(masterkey);
            var descendants = _contentLoader.GetDescendents(e.ContentLink);
            foreach (var contentLink in descendants)
            {
                var page = _contentLoader.Get<IContent>(contentLink);
                masterkey = GetMasterKey(page);
                _cache.RemoveLocal(masterkey);
            }
            masterkey = GetMasterKeyForAncestor(e.ContentLink);
            _cache.RemoveLocal(masterkey);
            var ancestors = _contentLoader.GetAncestors(e.ContentLink);
            foreach (var ancestor in ancestors)
            {
                masterkey = GetMasterKeyForAncestor(ancestor.ContentLink);
                _cache.RemoveLocal(masterkey);
            }
      }
}
//Set up some content events. 
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class PageEventsModule : IInitializableModule
{
        private static EPiServer.Logging.ILogger _log = LogManager.GetLogger(typeof(PageEventsModule));
        private ICacheManager _cacheManager;

        public void Initialize(InitializationEngine context)
        {
            // Configure the log4net.
            XmlConfigurator.Configure();
            _cacheManager = ServiceLocator.Current.GetInstance<ICacheManager>();
            var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
            contentEvents.PublishedContent += Instance_ContentChanged;
            contentEvents.MovedContent += Instance_ContentChanged;
            contentEvents.DeletedContent += Instance_ContentChanged;
            contentEvents.SavedContent += Instance_ContentChanged;
            contentEvents.MovingContent += Instance_ContentChanged;
        }
        

        void Instance_ContentChanged(object sender, ContentEventArgs e)
        {
            _cacheManager.OnContentChange(sender,e);
        }

        public void Uninitialize(InitializationEngine context)
        {
            var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
            contentEvents.PublishedContent -= Instance_ContentChanged;
            contentEvents.MovedContent -= Instance_ContentChanged;
            contentEvents.DeletedContent -= Instance_ContentChanged;
            contentEvents.SavedContent -= Instance_ContentChanged;
            contentEvents.MovingContent -= Instance_ContentChanged;
        }

        public void Preload(string[] parameters)
        {

        }
}

Now I can happily cache my items like this:

 var cacheEviction = cacheManager.GetCacheEvictionPolicy(new TimeSpan(0, 0, 10, 0),
                new[] { typeof(StandardPage) }, new[] {new ContentReference(6) });
 cache.Insert(cacheKey, cachedItem, cacheEviction);

...and if anything below the content node with id 6 is changed or any page of type "StandardPage" is changed, then the cache is invalidated.

The concept of master keys is really useful as you can see. It lets you clear parts of the cache easily and you can create powerful cache invalidation with ease.

Happy coding!

May 13, 2016

Comments

Dec 14, 2017 11:24 AM

There is a subtle bug in the code. The event DeletedContent gets triggered after the content has been deleted, this means that ContentEventArgs.Content will be null; causing a null reference exception to be thrown in the GetMasterKey() method. Maybe the DeletingContent event is a better candidate as it fires when a content item is about to be deleted permanently?

Dec 18, 2017 11:06 AM

Great catch! I'll update the relevant code :)

Tien Quach
Tien Quach Aug 21, 2018 06:17 AM

Just a small fix in GetCacheEvictionPolicy method that I think we should check null for "roots" parameter instead of "ancestorKeys". 

Emil Lundin
Emil Lundin Nov 9, 2018 08:31 AM

Hi! A bit late to the party here, but may I ask why you call _cache.RemoveLocal instead of _cache.Remove? Wouldn't this lead to an unsynchronized cache in a load balanced environment? Thanks in advance.

Please login to comment.
Latest blogs
The missing globe can finally be installed as a nuget package!

Do you feel like you're dying a little bit every time you need to click "Options" and then "View on Website"? Do you also miss the old "Globe" in...

Tomas Hensrud Gulla | Feb 14, 2025 | Syndicated blog

Cloudflare Edge Logs

Optimizely is introducing the ability to access Cloudflare's edge logs, which gives access to some information previously unavailable except throug...

Bob Davidson | Feb 14, 2025 | Syndicated blog

Comerce Connect calatog caching settings

A critical aspect of Commerce Connect is the caching mechanism for the product catalog, which enhances performance by reducing database load and...

K Khan | Feb 14, 2025

CMP DAM asset sync to Optimizely Graph self service

The CMP DAM integration in CMS introduced support for querying Optimizly Graph (EPiServer.Cms.WelcomeIntegration.Graph 2.0.0) for metadata such as...

Robert Svallin | Feb 13, 2025

PageCriteriaQueryService builder with Blazor and MudBlazor

This might be a stupid idea but my new years resolution was to do / test more stuff so here goes. This razor component allows users to build and...

Per Nergård (MVP) | Feb 10, 2025

Enhancing Optimizely CMS Multi-Site Architecture with Structured Isolation

The main challenge of building an Optimizely CMS website is to think about its multi site capabilities up front. Making adjustment after the fact c...

David Drouin-Prince | Feb 9, 2025 | Syndicated blog