Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Daniel Ovaska
May 13, 2016
  6655
(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
Routing to a page in SaaS CMS

More early findings from using a SaaS CMS instance; setting up Graph queries that works for both visitor pageviews and editor previews.

Johan Kronberg | Apr 14, 2025 |

Developer Meetup - London, 24th April 2025

Next Thursday, 24th April will be Candyspace 's first Optimizely Developer Meetup, and the first one held in London this year! We've have some...

Gavin_M | Apr 14, 2025

Successful Digitalization for SMEs: How Optimizely One can Revolutionize Your Business Processes

"Achieve digital excellence with Optimizely One: Boost efficiency, delight customers, secure growth." In today's digital world, it's crucial for...

Frank Hohmeyer | Apr 11, 2025

Personalized Optimizely CMS Website Search Experiences Azure AI Search & Personalizer

In the last blog, we discussed Integrating the Optimizely CMS website with Azure AI search. Now let’s take a bit more advanced topic to serve...

Naveed Ul-Haq | Apr 10, 2025 |