Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

Daniel Ovaska
May 13, 2016
  6513
(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
Best Bets, synonyms and wildcard queries

When using a wildcard in your search query like ‘search.For(query+”*”)’ or when you used the reversed method suggested in the Breaking changes...

Jeroen Stemerdink | Jan 28, 2025 | Syndicated blog

Content statistics Blazor component

Another week and another MudBlazor component to explore. I wanted to test the charts components so I created a small Blazor component that displays...

Per Nergård (MVP) | Jan 28, 2025

Referencing Page Specific Blocks with ISelectionFactory

A content modeling exercise got me thinking about reuse of page-specific content. It turns out that Optimizely has some good tools to handle this...

Nicholas Sideras | Jan 28, 2025 | Syndicated blog

Further Enhancing Production Database Scalability in DXP Cloud Services

About a year ago we announced that we are improving the scalability of SQL databases in DXP Cloud Services , focusing first on non-production...

Anna Pleshakova | Jan 27, 2025