Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jay 409 posts 635 karma points
    Jul 20, 2016 @ 22:25
    Jay
    0

    Donut caching on Load Balance environment

    Hi All,

    Was wondering if anyone had used donut caching on a load balance environment before. Was wondering if there's any additional setup that i need to do / worry about especially the cache clearing bit on a load balance environment. I have already got an event that clear off the cache whenever there is a publish/delete/etc.

    Appreciate any help on this. Thanks

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 21, 2016 @ 07:05
    Jeavon Leopold
    0

    Yes lots of times, the clear events are key, what events are you using?

  • Jay 409 posts 635 karma points
    Jul 21, 2016 @ 07:33
    Jay
    0

    Hi Jeavon,

    I'm using the below to clear off the cache. Do i need to change it for a load balance environment? Thanks

    public class OutputCacheClearerEvents : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            { }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                content.AfterRefreshContent += ContentServiceOnEvent;
                ContentService.Published += ContentServiceOnEvent;
                ContentService.UnPublished += ContentServiceOnEvent;
                ContentService.Deleted += ContentServiceOnEvent;
                ContentService.Moved += ContentServiceOnEvent;
                ContentService.Trashed += ContentServiceOnEvent;
                MediaService.Saved += MediaServiceOnEvent;
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            { }
    
    
            private static void ContentServiceOnEvent(object sender, object e)
            {
                ClearCache();
            }
    
            private static void MediaServiceOnEvent(object sender, object e)
            {
                ClearCache();
            }
    
            private static void ClearCache()
            {
                try
                {
                    var cacheManager = new OutputCacheManager();
                    cacheManager.RemoveItems();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(typeof(OutputCacheClearerEvents), "Error clearing cache", ex);
                }
            }
        }
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 21, 2016 @ 14:24
    Jeavon Leopold
    8

    Those events are not going to occur on all servers. Instead you should use the cache events as shown below (you should also be using ApplicationEventHandler)

    namespace MyProject.Events
    {
        using System;
    
        using Umbraco.Core.Cache;
        using Umbraco.Core.Logging;
        using Umbraco.Web.Cache;
        using Umbraco.Core;
    
        using DevTrends.MvcDonutCaching;
    
        public class UmbracoEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                CacheRefresherBase<PageCacheRefresher>.CacheUpdated += this.CachePageEvents_CacheUpdated;
                CacheRefresherBase<DictionaryCacheRefresher>.CacheUpdated += this.CacheDictionaryEvents_CacheUpdated;
                CacheRefresherBase<MediaCacheRefresher>.CacheUpdated += this.CacheMediaEvents_CacheUpdated;
            }
    
            private void CacheMediaEvents_CacheUpdated(MediaCacheRefresher sender, CacheRefresherEventArgs e)
            {
                ClearDonutOutputCache();
            }
    
            private void CacheDictionaryEvents_CacheUpdated(DictionaryCacheRefresher sender, CacheRefresherEventArgs e)
            {
                ClearDonutOutputCache();
            }
    
            private void CachePageEvents_CacheUpdated(PageCacheRefresher sender, CacheRefresherEventArgs e)
            {
                ClearDonutOutputCache();
            }
    
            private void ClearDonutOutputCache()
            {
                var cacheManager = new OutputCacheManager();
                cacheManager.RemoveItems();
                LogHelper.Info<UmbracoEvents>(string.Format("Donut Output Cache Cleared On Server:{0} AppDomain:{1}", Environment.MachineName, System.AppDomain.CurrentDomain.FriendlyName));
            }
        }
    }
    
  • Simon 692 posts 1068 karma points
    Jun 02, 2017 @ 11:00
    Simon
    0

    Thanks @Jeavon.

  • Jay 409 posts 635 karma points
    Jul 21, 2016 @ 15:11
    Jay
    1

    Nice. Thanks Jeavon ;)

  • Jay 409 posts 635 karma points
    Jul 22, 2016 @ 09:45
    Jay
    0

    Hey Jeavon,

    Quick question, is there a way i can check for a certain document type to be excluded from the CachePageEvents_CacheUpdated?

    Thanks

  • Jay 409 posts 635 karma points
    Jul 22, 2016 @ 16:52
    Jay
    0

    The reason is I've got a feed that update and publish a node every 20 minutes. So wanted to detect and exclude that node from clearing the cache if it's possible.

    Thanks

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jul 26, 2016 @ 11:17
    Jeavon Leopold
    4

    Yes you can, something like this:

    private void CachePageEvents_CacheUpdated(PageCacheRefresher sender, CacheRefresherEventArgs e)
    {
        if (e != null)
        {
            if (e.MessageObject != null)
            {
                var contentType = string.Empty;
    
                if (e.MessageType == MessageType.RefreshByInstance)
                {
                    var content = (Umbraco.Core.Models.Content) e.MessageObject;
                    contentType = content.ContentType.Alias;
                }
    
                if (e.MessageType == MessageType.RefreshById)
                {
    
                    var id = (int) e.MessageObject;
                    if (e.MessageType == MessageType.RefreshById)
                    {
                        var publishedNode = UmbracoContext.Current.ContentCache.GetById(id);
                        if (publishedNode != null)
                        {
                            contentType = publishedNode.DocumentTypeAlias;
                        }
                    }
    
                    if (!string.IsNullOrEmpty(contentType))
                    {
                        if (!contentType.InvariantEquals("MySpecialDocTypeAlias"))
                        {
                            ClearDonutOutputCache();
                        }
                    }
                }
            }
        }
    }
    
  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Jan 23, 2017 @ 09:01
    Søren Gregersen
    0
            if (e.MessageType == MessageType.RefreshById)
            {
    
                var id = (int) e.MessageObject;
                if (e.MessageType == MessageType.RefreshById)
                {
                    var publishedNode = UmbracoContext.Current.ContentCache.GetById(id);
                    if (publishedNode != null)
                    {
                        contentType = publishedNode.DocumentTypeAlias;
                    }
                }
    
                if (!string.IsNullOrEmpty(contentType))
                {
                    if (!contentType.InvariantEquals("MySpecialDocTypeAlias"))
                    {
                        ClearDonutOutputCache();
                    }
                }
            }
    

    may need to be:

            if (e.MessageType == MessageType.RefreshById)
            {
                var publishedNode = UmbracoContext.Current.ContentCache.GetById(e.MessageObject);
                if (publishedNode != null)
                {
                    contentType = publishedNode.DocumentTypeAlias;
                }
            }
    
            if (!string.IsNullOrEmpty(contentType))
            {
                if (!contentType.InvariantEquals("MySpecialDocTypeAlias"))
                {
                    ClearDonutOutputCache();
                }
            }
    
  • Jay 409 posts 635 karma points
    Jul 26, 2016 @ 22:03
    Jay
    0

    You're a superstar Jeavon ;)

  • Jay 409 posts 635 karma points
    Sep 05, 2016 @ 16:31
    Jay
    0

    Hey Jeavon quick question,

    Have you came across a way to rebuild indexes on a distributed environments?

    Eg when the Developer Section > Examine Management index gets rebuild on a single environment and it rebuilds on the rest of the environments

    Can't find anything in the forum

    Thanks

  • Tommy Enger 72 posts 277 karma points c-trib
    Oct 25, 2016 @ 07:36
    Tommy Enger
    0

    Hi JLon. Did you find a way to rebuild indexes in distributed environments?

  • Jay 409 posts 635 karma points
    Jun 16, 2017 @ 11:46
    Jay
    0

    Hey Tommy,

    Just saw this. You mean rebuild the entire indexes on individual load balancing environment?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jun 16, 2017 @ 11:48
    Jeavon Leopold
    1

    Reminded me, I did build a simple tool for this, it's on GitHub (no package currently) https://github.com/CrumpledDog/ExamineDistributedDashboard

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Jun 19, 2017 @ 12:40
    Simon Dingley
    0

    Looking forward to trying this out - it's something I've had on my list for a long time and never had the chance to investigate further! Thanks

  • Poornima Nayar 106 posts 276 karma points MVP 5x c-trib
    Oct 22, 2017 @ 19:28
    Poornima Nayar
    0

    Hi,

    Is there any way to achieve Donut caching but not clear all cache items everytime we publish? Wont this cause an overhead on servers which are constantly publishing?

    Poornima

Please Sign in or register to post replies

Write your reply to:

Draft